I am working on study drill #5 for exercise 35 and can’t figure out a better way to allow any integer to be entered as an input than this:
if “0” in choice or “1” in choice or “2” in choice or “3” in choice or “4” in choice or “5” in choice or “6” in choice or “7” in choice or “8” in choice or “9” in choice:
Does anyone know how to simplify this?
Here is the entire block of code for reference:
def gold_room():
print(“This room is full of gold. How much do you take?”)
choice = input("> ")
if "0" in choice or "1" in choice or "2" in choice or "3" in choice or "4" in choice or "5" in choice or "6" in choice or "7" in choice or "8" in choice or "9" in choice:
how_much = int(choice)
else:
dead("Learn to type a number!")
if how_much < 50:
print("You are not greedy. You win!")
exit(0)
else:
dead("You greedy bastard!")
Thanks @DidierCH and @zedshaw! I got it to work using the str.isdigit library. I’ll go back and review the range function because its something I’ve gone through before, but I guess it didn’t stick… Loving lp3thw as an intro to coding by the way.
Thank you for your advice, but I totally confused when I put this code into script:
from sys import exit
def gold_room():
print("This room is full of gold. How much do you take?")
choice = input("> ")
if choice in range(10):
how_much = int(choice)
else:
dead("Man, learn to type a number")
if how_much < 50:
print("Nice, you're not greedy, you win!")
else:
dead("You greedy bastard!")
def dead(why):
print(why, "Good job!")
exit(0)
gold_room()
I have tried for several times but the only Boolean value I can get from this is False
if choice in range(10):
how_much = int(choice)
I used to think I get a string like thing from input(), so I tried some code this kind of form:
a = "1"
a1 = input("> ")
b = [1, 2, 3]
c = a in b
print(c)
c1 = a1 in b
print(c1)
I use a list to represent range(start, stop) then I got the result that:
“1” in [1, 2, 3] >>>False
input() in [1, 2, 3]>>>False
“1” in [“1”, 2, 3]>>> True
“12” in [1, 2, 3]>>>False(if length of number you type in greater than 10 )
Do I miss something important? @zedshaw
It’s pleased to hear from you:)
I learned a new function’ type’ from your pic and it’s really useful for me. I think what you may want to say is change the code :
if choice in range(10):>>>>>>>>>>>>>>>>>>if int(choice) in range(10):
So I try it again and there are still some bugs: if you input a number which has ten’s digit or hundred’s digit or even more will cause the Boolean expression return False like I said before:
You should check out Built-in Types — Python 3.9.7 documentation
I solved it with str.isdigit().
It is also recommended that you learn to use python’s help documentation to find out which functions or modules have not been used and validate them in Python!
i tried by range() method but this fails the if condition all the time if choice is in range or not in range
the only method which is working right now is given by @andreotte as far as i know
but i tried one way which is mess and also not working
a = list(range(101))
b = []
for i in a:
b.append(str(i))
if choice in b:
how_much = int(choice)
You don’t need the a variable. You can just write:
for i in range(101):
pass
Do you search for numbers or strings? Don’t mix ‘strings’ and ‘numbers’, Python makes a strict distinction between the two. You append strings to b when you write b.append(str(i)) wherefore choice needs to be a string too, like choice = "3"
@zedshaw@DidierCH the thing is that the input is not an integer [ int() ] so it will accept the input as a string and NOT an INT(). it will always go to : (man learn to type a number): ill post my code migh have a problem though:
choice = input("> ")
if choice in range(1000):
how_much = int(choice)
else:
dead("Man, learn to type a number.")
You would need to convert it to a number first, then if you want to check that it’s in a certain range you’d just use inequality. I’ll give you some notes (not real code) and you can take a look at it as a possible solution:
choice = input
if choice.isdigit:
# convert it to a number
# check it's < 0 and > 1000
else:
dead("Learn to type a number")
The other way to do it is to do int() and then catch the exception:
try:
choice = int(input("> "))
except:
# it's not a number
This code isn’t functional, it’s just fake code. You can use that to then work out your own solution.