I'm doing Learn Python the Hard Way exercise 35. Below is the original code, and we're asked to change it so it can accept numbers that don't have just 0 and 1 in them.
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!")
This is my solution, which runs fine and recognizes float values:
def gold_room():
print "This room is full of gold. What percent of it do you take?"
next = raw_input("> ")
try:
how_much = float(next)
except ValueError:
print "Man, learn to type a number."
gold_room()
if how_much <= 50:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")
Searching through similar questions, I found some answers that helped me write another solution, shown in the below code. The problem is, using isdigit() doesn't let the user put in a float value. So if the user said they want to take 50.5%, it would tell them to learn how to type a number. It works otherwise for integers. How can I get around this?
def gold_room():
print "This room is full of gold. What percent of it do you take?"
next = raw_input("> ")
while True:
if next.isdigit():
how_much = float(next)
if how_much <= 50:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!")
else:
print "Man, learn to type a number."
gold_room()
RE would be a good choice
If the raw input is a float number, it will return an object. Otherwise, it will return None. In case you need to recognize the int, you could:
isinstance(next, (float, int))
will do the trick simply ifnext
is already converted from a string. It isn't in this case. As such you would have to usere
to do the conversion if you want to avoid usingtry..except
.I would recommend using the
try..except
block that you had before instead of aif..else
block, but putting more of the code inside, as shown below.This will try to cast it as a float and if it fails will raise a
ValueError
that will be caught. To learn more, see the Python Tutorial on it.You can use a regex to validate the format:
You can found the documentation here: https://docs.python.org/2/library/re.html
The problem I have with your approach is that you're going down a path of "Look Before You Leap" instead of the more Pythonic "Easier to Ask Forgiveness than Permission" path. I think your original solution is better than trying to validate the input in this way.
Here is how I would write it.
use below python based regex checking for float strings
output: !None, !None, !None, None , !None then use this output to do conversions.