Ex35 gold_room() input


#1

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!")

#2

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


#3

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

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


Ex 35 - Study Drills solutions?
#4

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.


#5

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:

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


#6

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:
    dead("You greedy bastard!")

#7

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


#8


#9

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

1


#10

You should check out https://docs.python.org/3/library/stdtypes.html?highlight=range#range
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!


#11

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


#12

他的这个代码页运行不出预想的结果丫 但是很感谢你的帮助


#13

答案有效,我只是尝试过。 此外,您需要在此论坛上用英语回复,以便人们了解您。