[英]How can I limit the user input to only certain integers (ie: Only between 1-100, which also includes 1 and 100) in Python
我正在嘗試為我的計算作業制作一個猜數字游戲,但我不知道如何限制用戶只能輸入某些整數(即:1-100,還包括 1 和 100)這是我目前的代碼:
import random
BOUNDS = (1, 100)
TRIES_ALLOWED = 6
the_number = random.randint(*BOUNDS)
print("\tWelcome to 'Guess My Number'!\n")
print("I'm thinking of a number between %d and %d." % BOUNDS)
print("Try to guess it in as few attempts as possible.")
print("You only have 6 attempts to guess the number.\n")
for tries in range(TRIES_ALLOWED):
guess = int(input("Take a guess: "))
if guess > 100:
print ('That is an error please enter a number between 1 and 100')
if guess < 1:
print ('That is an error please enter a number between 1 and 100')
if guess > the_number:
print("The number is lower")
elif guess < the_number:
print("The number is higher")
else:
print("You guessed it! The number was %d" % (the_number))
print("And it only took you %d tries!\n\n" % (tries + 1))
break
else:
print("You failed to guess in time!\n\n")
如果你能幫助我,我將不勝感激
您可以嘗試使用此基本代碼來選擇 1 到 100 之間的數字
import random
num = random.randint(1, 100)
while True:
print('Guess a number between 1 and 100')
guess = input()
i = int(guess)
if i == num:
print('You won!!!')
break
elif i < num:
print('Try Higher')
elif i > num:
print('Try Lower')
#any recommendations for the game end
print('if you gussed less than 6 times you won')
def prompt_number_range(start, end):
while True:
prompt_str = "Please enter a number within the range {0} - {1}:".format(start, end)
try:
user_input = raw_input(prompt_str)
except NameError:
user_input = input(prompt_str)
try:
input_as_int = int(user_input)
if start <= input_as_int <= end:
return input_as_int
else:
print("that is not within the given boundaries")
except ValueError:
pass
print (prompt_number_range(1,100))
您可以在此處查看上述內容:
https://ideone.com/9mtoOt(Python 3)
https://ideone.com/W6hRGh(Python 2.7)
以下是上面的代碼合並到您的代碼中完全正常運行:
import random
def prompt_number_range(start, end):
while True:
prompt_str = "Please enter a number within the range {0} - {1}:".format(start, end)
try:
user_input = raw_input(prompt_str)
except NameError:
user_input = input(prompt_str)
try:
input_as_int = int(user_input)
if start <= input_as_int <= end:
return input_as_int
else:
print("that is not within the given boundaries")
except ValueError:
pass
BOUNDS = (1, 100)
TRIES_ALLOWED = 6
the_number = random.randint(*BOUNDS)
print("\tWelcome to 'Guess My Number'!\n")
print("I'm thinking of a number between %d and %d." % BOUNDS)
print("Try to guess it in as few attempts as possible.")
print("You only have 6 attempts to guess the number.\n")
for tries in range(TRIES_ALLOWED):
user_guess = prompt_number_range(*BOUNDS)
if user_guess == the_number:
print("You guessed it! The number was %d" % (the_number))
print("And it only took you %d tries!\n\n" % (tries + 1))
break
elif the_number < user_guess:
print("The number is lower")
elif the_number > user_guess:
print("The number is higher")
else: #for loop finished normally without breaking
print("You failed to guess in time!\n\n")
嘗試使用:
import random
BOUNDS = (1, 100)
TRIES_ALLOWED = 6
win = False
the_number = random.randint(*BOUNDS)
print("\tWelcome to 'Guess My Number'!\n")
print("I'm thinking of a number between %d and %d." % BOUNDS)
print("Try to guess it in as few attempts as possible.")
print("You only have 6 attempts to guess the number.\n")
for tries in range(TRIES_ALLOWED):
guess = int(input("Take a guess: "))
if guess > 100:
print ('That is an error please enter a number between 1 and 100')
continue
elif guess < 1:
print ('That is an error please enter a number between 1 and 100')
continue
if guess > the_number:
print("The number is lower")
elif guess < the_number:
print("The number is higher")
elif guess == the_number:
win = True
break
if win == True:
print("You guessed it! The number was %d" % (the_number))
print("And it only took you %d tries!\n\n" % (tries + 1))
elif win == False:
print("You failed to guess in time!\n\n")
這是你要求的嗎?
>>> number1 = -1
>>> number2 = 1
>>> number3 = 100
>>> number4 = 101
>>> number5 = 34
>>> BOUNDS = (1, 100)
>>> for number in number1, number2, number3, number4, number5:
... print(number in range(BOUNDS[0], BOUNDS[1] + 1))
...
False
True
True
False
True
如果此代碼的唯一問題是輸入驗證,則可以替換整個塊:
guess = int(input("Take a guess: "))
if guess > 100:
print ('That is an error please enter a number between 1 and 100')
if guess < 1:
print ('That is an error please enter a number between 1 and 100'
有了這個:
guess = None
while guess not in range(1,101):
guess = int(raw_input("Enter a number between 1 and 100:"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.