# Ex35 gold_room() input

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:

Hi @andreotte you can do this:

``````if choice in range(10): # every number from 0 to 9 is true
how_much = int(choice)
``````

if you need to verify something in a different range, you can do this:

``````if choice in range(5, 15): # every number from 5 to 14 is true
how_much = int(choice)
``````

It’s worth to study the range function, you can do a lot of cool stuff with it: https://docs.python.org/3.7/library/stdtypes.html#range

2 Likes

Yes, @DidierCH proposal is correct, however I think what you really want is this:

https://docs.python.org/3/library/stdtypes.html#str.isdigit

2 Likes

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.

Great, if you get to OOP and it’s too hard step back and look at:

https://tv.learncodethehardway.com/videos/search?search=OOPSchool

Which has code at:

https://lab.learncodethehardway.com/zedshaw/oopschool

Do that and then try OOP again and it might make more sense.

Can also be achieved using try and except

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

``````choice = input("> ")
try:
how_much = int(choice)
except:
dead("Man, learn to type a number.")

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

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:

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:

``````12 in [1, 2, 3]>>>>>False
`````` 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!

1 Like

Using `isdigit()` is the best solution here. You can also attempt to use int(choice), and then use Python exceptions to catch when it is an error:

https://docs.python.org/3/tutorial/errors.html

1 Like

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)
``````

My fifty cents:

1. You don’t need the `a` variable. You can just write:
``````for i in range(101):
pass
``````
1. 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.

1 Like

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

``````                                #choice = int(input("> "))
how_much = int(input('>> '))

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