如果输出为假,则重复输入
Repeating an input if the output is false
我想在 python 中制作一个 "guess the number" 游戏,我在其中选择最小和最大数字,如果我选择的数字较低或较高,我希望它重复问题,如何才能我这样做?
这是我的代码:
import random
import time
print("Welcome to the guessing game!")
time.sleep(1)
print("Choose your minumum number")
minnum=input("Min: ")
print(" ")
print("Choose your maximum number")
maxnum=input("Max: ")
print(" ")
print("Enter your number")
num = input("Number: ")
print(" ")
q = random.randint(int(minnum), int(maxnum))
def game():
if int(num) < q:
print("The number is higher")
if int(num) > q:
print("The number is lower")
if int(num) == q:
print("Congratulations! you won!")
break
game()
print(" ")
print(" ")
input("Press enter to exit")
在 game() 中移动输入并使其成为一个循环:
def game():
while True:
print("Enter your number")
num = input("Number: ")
if int(num) < q:
print("The number is higher")
if int(num) > q:
print("The number is lower")
if int(num) == q:
print("Congratulations! you won!")
break
你需要一个 while
循环来继续如果用户输入了错误的猜测并且如果正确则循环将退出 break
:
import random
import time
print("Welcome to the guessing game!")
time.sleep(1)
print("Choose your minumum number")
minnum=input("Min: ")
print(" ")
print("Choose your maximum number")
maxnum=input("Max: ")
print(" ")
q = random.randint(int(minnum), int(maxnum))
def game():
while True: #while loop for looping continuously until correct input
print("Enter your number")
num = input("Number: ")
print(" ")
if int(num) < q:
print("The number is higher")
if int(num) > q:
print("The number is lower")
if int(num) == q: #if answer correct stop looping
print("Congratulations! you won!")
break
game()
print(" ")
print(" ")
input("Press enter to exit")
输出:
Welcome to the guessing game!
Choose your minumum number
Min: 1
Choose your maximum number
Max: 5
Enter your number
Number: 3
The number is lower
Enter your number
Number: 4
The number is lower
Enter your number
Number: 5
The number is lower
Enter your number
Number: 2
Congratulations! you won!
如果您愿意,此变体会执行额外的验证:
from random import randint
def request_user_input(query):
while True:
try:
return int(input(query))
except ValueError:
continue
def run_game(target):
while True:
guess = request_user_input('Enter your number: ')
if guess < target:
print('The number is higher')
elif guess > target:
print('The number is lower')
else:
print('Congratulations! You won!')
break
if __name__ == '__main__':
min = request_user_input('Min: ')
max = request_user_input('Max: ')
if max < min:
raise ValueError('The maximum value cannot be smaller than the minimum')
run_game(target=randint(min, max + 1))
例子运行
Min: a
Min: 10
Max: 20
Enter your number: 5
The number is higher
Enter your number: 25
The number is lower
Enter your number: 15
The number is higher
Enter your number: 18
The number is higher
Enter your number: 20
The number is lower
Enter your number: 19
Congratulations! You won!
我想在 python 中制作一个 "guess the number" 游戏,我在其中选择最小和最大数字,如果我选择的数字较低或较高,我希望它重复问题,如何才能我这样做?
这是我的代码:
import random
import time
print("Welcome to the guessing game!")
time.sleep(1)
print("Choose your minumum number")
minnum=input("Min: ")
print(" ")
print("Choose your maximum number")
maxnum=input("Max: ")
print(" ")
print("Enter your number")
num = input("Number: ")
print(" ")
q = random.randint(int(minnum), int(maxnum))
def game():
if int(num) < q:
print("The number is higher")
if int(num) > q:
print("The number is lower")
if int(num) == q:
print("Congratulations! you won!")
break
game()
print(" ")
print(" ")
input("Press enter to exit")
在 game() 中移动输入并使其成为一个循环:
def game():
while True:
print("Enter your number")
num = input("Number: ")
if int(num) < q:
print("The number is higher")
if int(num) > q:
print("The number is lower")
if int(num) == q:
print("Congratulations! you won!")
break
你需要一个 while
循环来继续如果用户输入了错误的猜测并且如果正确则循环将退出 break
:
import random
import time
print("Welcome to the guessing game!")
time.sleep(1)
print("Choose your minumum number")
minnum=input("Min: ")
print(" ")
print("Choose your maximum number")
maxnum=input("Max: ")
print(" ")
q = random.randint(int(minnum), int(maxnum))
def game():
while True: #while loop for looping continuously until correct input
print("Enter your number")
num = input("Number: ")
print(" ")
if int(num) < q:
print("The number is higher")
if int(num) > q:
print("The number is lower")
if int(num) == q: #if answer correct stop looping
print("Congratulations! you won!")
break
game()
print(" ")
print(" ")
input("Press enter to exit")
输出:
Welcome to the guessing game!
Choose your minumum number
Min: 1
Choose your maximum number
Max: 5
Enter your number
Number: 3
The number is lower
Enter your number
Number: 4
The number is lower
Enter your number
Number: 5
The number is lower
Enter your number
Number: 2
Congratulations! you won!
如果您愿意,此变体会执行额外的验证:
from random import randint
def request_user_input(query):
while True:
try:
return int(input(query))
except ValueError:
continue
def run_game(target):
while True:
guess = request_user_input('Enter your number: ')
if guess < target:
print('The number is higher')
elif guess > target:
print('The number is lower')
else:
print('Congratulations! You won!')
break
if __name__ == '__main__':
min = request_user_input('Min: ')
max = request_user_input('Max: ')
if max < min:
raise ValueError('The maximum value cannot be smaller than the minimum')
run_game(target=randint(min, max + 1))
例子运行
Min: a
Min: 10
Max: 20
Enter your number: 5
The number is higher
Enter your number: 25
The number is lower
Enter your number: 15
The number is higher
Enter your number: 18
The number is higher
Enter your number: 20
The number is lower
Enter your number: 19
Congratulations! You won!