• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Python Problem , no one can help me

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi please help.
Write a Python program that reads an integer representing a month of the year, and then print the name of the month. Your program must include validation and error handling code. Thus, if the user enters a value outside the range of 1-12; or characters that cannot be converted to an integer, your code must print the appropriate message and continue without crashing. (Hint: You may NOT USE any 'if' statements.)    
 
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What do you have so far, and where are you stuck making progress? We'll try to help you, but we won't write the code for you.
 
Marshal
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please show us how far have you got. Please post the code if you wrote already some, or write down the algorithm steps you designed on a piece of paper.
 
Logan Cherry
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Welcome to the Ranch.

Please show us how far have you got. Please post the code if you wrote already some, or write down the algorithm steps you designed on a piece of paper.

Help.png
This is what ive done so far . I am new to coding and trying to learn python .
This is what ive done so far . I am new to coding and trying to learn python .
 
Liutauras Vilda
Marshal
Posts: 8988
652
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please post actual code as a text rather than picture.
 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Logan Cherry wrote:(Hint: You may NOT USE any 'if' statements.)



I find that rather silly as a requirement and bizarre as a "Hint".
 
Bartender
Posts: 209
14
Mac OS X IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anand Hariharan wrote:

Logan Cherry wrote:(Hint: You may NOT USE any 'if' statements.)



I find that rather silly as a requirement and bizarre as a "Hint".



It might make sense in a learning environment I guess, you don't really need "ifs" for this assignment.

Logan, Python doesn't have a "switch" statement afaik so I guess you are supposed to use a dictionary, you almost got it right, google the syntax and; also, your program is supposed to read integers
 
Anand Hariharan
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To a beginner to programming, would you really teach exception handling and KeyError in dictionary access, before you teach if statement?
 
Adrian Grabowski
Bartender
Posts: 209
14
Mac OS X IntelliJ IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anand Hariharan wrote:To a beginner to programming, would you really teach exception handling and KeyError in dictionary access, before you teach if statement?



I assume that they were taught "if" statements already and now are moving into more advanced concepts. I might be wrong.
 
Marshal
Posts: 80085
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would consider teaching beginners how to throw exceptions before they learn to catch them. You know, like this:-...but that is awkward to do without a selection of some sort.

[addition]That is Java® code, not Python
 
Greenhorn
Posts: 8
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I am hoping that I'm not giving away too much here, but this is only my first answer on this site, so I am hoping a moderator will let me know if I am within the bounds of what is considered an acceptable amount of help. Anyhow, here is a code snipit that I created to test if the variable x is equal to 1 without using any if statements. It should at least give you a way to get around the rather odd requirement of not using if statements. It also seems to handle some of the exception handling as well. I have no idea if this is anything like what your assignment is looking for, but I hope it gives you a starting point. You will still need to figure out how to get the user's input into the variable x, and expand the program as necessary to handle all the other various cases. You will also need to ensure that it is in line with what your assignment is looking for, and that it works correctly. You should also check to see if your course has even covered the applicable subject material yet. If you haven't yet covered lambda or the "pass" keyword, this may not be what your assignment is looking for. I have also included the sources of where I found the various pieces that I used to build this snipit, so you should check those out as well to see how I figured out how to put the pieces together.

Anyhow, without further ado, here is the code snipit and sources and description:



Also, I tested this code snipit in Python version 3.6.9, so hopefully it should work on your machine.

I hope that helps.
 
Campbell Ritchie
Marshal
Posts: 80085
412
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roger Frybarger wrote:. . . an acceptable amount of help. . . .

Since the question was asked on Tuesday, and it was Monday (at least in my time zone) by the time you posted your solution, I cannot see that posting a complete answer can do any harm.
 
Roger Frybarger
Greenhorn
Posts: 8
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Since the question was asked on Tuesday, and it was Monday (at least in my time zone) by the time you posted your solution, I cannot see that posting a complete answer can do any harm.



Awesome! Thanks Campbell Ritchie!    That helps me feel better about what, how, and when to post. Actually that gives me an idea: I may scout out old posts that don't seem to include a conclusion and provide some kind of complete answer of sorts. We'll see if I have the time for that, but I would certainly enjoy tying up loose ends in that way to aid future readers. Anyhow, thanks again.  
 
Campbell Ritchie
Marshal
Posts: 80085
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roger Frybarger wrote:. . . old posts that don't seem to include a conclusion . . .  

That sounds like a good idea
 
Slime does not pay. Always keep your tiny ad dry.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic