Re: [Tutor] help with user input

2011-03-21 Thread Donald Bedsole
Thank you, Marc

On Mon, Mar 21, 2011 at 4:47 PM, Marc Tompkins  wrote:
> On Mon, Mar 21, 2011 at 1:12 PM, Donald Bedsole  wrote:
>>
>> This works fine as long as the user enters a number.  However, if they
>> enter anything else, they just get the first :else statement, "You
>> were too greedy."
>>
> I think that's because you're trying to do a string comparison, rather than
> a numeric comparison. (if next <= "50":)  You need to convert 'next' to an
> int FIRST, then compare to 50, not "50".
>
>>
>> My googling found solutions using an exception, but that hasn't been
>> introduced yet in the tutorial.  How would you solve this without
>> using an exception?
>>
>
> If you don't want to use an exception, check the entered value first (note:
> I haven't checked my code, so caveat lector) -
>
>> next = raw_input(">")
>> if next.isdigit():
>>     if int(next) < 50:
>>     print "Nice, you're not greedy, you win!"
>>     else:
>>     dead("You were too greedy.")
>> else:
>>     dead("Man, learn to type a number!")
>
> isdigit() returns True if every character is a digit; False otherwise.
> http://docs.python.org/library/stdtypes.html
>
>
> Using an exception:
>
>> next = raw_input(">")
>> try:
>>     if int(next) < 50:
>>     print "Nice, you're not greedy, you win!"
>>     else:
>>     dead("You were too greedy.")
>> except ValueError:
>>     dead("Man, learn to type a number!")
>
> Note that I specified ValueError - you want to make your exception handling
> as specific as possible, so that if really unforeseen things go wrong, your
> program doesn't blindly treat them as normal.  In other words, if any
> exception other than ValueError were to pop up here, you would want the
> program to terminate and show you a traceback so you could fix it.
>
>
>

It'll take me awhile to digest this.  Thanks for your time and your help.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with user input

2011-03-21 Thread Marc Tompkins
On Mon, Mar 21, 2011 at 1:12 PM, Donald Bedsole  wrote:

>
> This works fine as long as the user enters a number.  However, if they
> enter anything else, they just get the first :else statement, "You
> were too greedy."
>
> I think that's because you're trying to do a string comparison, rather than
a numeric comparison. (if next <= "50":)  You need to convert 'next' to an
int FIRST, then compare to 50, not "50".


> My googling found solutions using an exception, but that hasn't been
> introduced yet in the tutorial.  How would you solve this without
> using an exception?
>
>
If you don't want to use an exception, check the entered value first (note:
I haven't checked my code, so caveat lector) -

next = raw_input(">")
> if next.isdigit():
> if int(next) < 50:
> print "Nice, you're not greedy, you win!"
> else:
> dead("You were too greedy.")
> else:
> dead("Man, learn to type a number!")
>

isdigit() returns True if every character is a digit; False otherwise.
http://docs.python.org/library/stdtypes.html


Using an exception:

next = raw_input(">")
> try:
> if int(next) < 50:
> print "Nice, you're not greedy, you win!"
> else:
> dead("You were too greedy.")
> except ValueError:
> dead("Man, learn to type a number!")
>

Note that I specified ValueError - you want to make your exception handling
as specific as possible, so that if really unforeseen things go wrong, your
program doesn't blindly treat them as normal.  In other words, if any
exception other than ValueError were to pop up here, you would want the
program to terminate and show you a traceback so you could fix it.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] help with user input

2011-03-21 Thread Donald Bedsole
I'm going through a tutorial called "Learn Python the Hard Way" by Zed
Shaw.  At the end of his lessons he has "Extra Credit" sessions, and
I'm stuck on one.

I'm on lesson 35, here is a link to it:

http://blamcast.net/python/ex35.html

The lesson involves creating a very simple text based game.  One of
the functions accepts user input:

def gold_room():
print "This room is full of gold.  How much do you take?"

next = raw_input("> ")
if "0" in next or "1" in next:
how_much = int(next)
else:
dead("Man, learn to type a number.")

if how_much < 50:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")


The instruction from the Extra Credit section reads:

The gold_room has a weird way of getting you to type a number. What
are all the bugs in this way of doing it? Can you make it better than
just checking if "1" or "0" are in the number? Look at how int() works
for clues.


I have read the documentation for int() and done some googling, but no
lights have come on. :-)

Here is what I have so far:

def gold_room():
print "This room is full of gold. How much do you take?"

next = raw_input("> ")
if next <= "50":
how_much = int(next)

else:
dead("You were too greedy.")

if how_much < 50:
print "Nice, you're not greedy, you win!"
exit(0)

else:
dead("Man, learn to type a number!")


This works fine as long as the user enters a number.  However, if they
enter anything else, they just get the first :else statement, "You
were too greedy."

My googling found solutions using an exception, but that hasn't been
introduced yet in the tutorial.  How would you solve this without
using an exception?

Thanks for any help,

Don
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor