Getting an Integer Input in a Range

后端 未结 5 547
鱼传尺愫
鱼传尺愫 2021-01-03 11:30

I\'m trying to take a raw input and detect whether it is in a range.
Here\'s my code.

def gold_room():
    print \"This room is full of gold. How much          


        
5条回答
  •  南笙
    南笙 (楼主)
    2021-01-03 12:23

    The result of raw_input() will be a string, so first you need to check to see if next is all digits. There are a few ways to do this, the easiest is to use the str.isdigit() function:

    next = raw_input("> ")
    if next.isdigit():
        how_much = int(next)
    else:
        dead("Man, learn how to type a number.")
    

    Note that you do not need to check to see if the value for next is in the range from 0 to 50, since your next if statement already checks to see if the value is less than 50 and negative numbers will be excluded by next.isdigit().

提交回复
热议问题