Python 如果输出为false,则重复输入
我想用python做一个“猜数字”游戏,我选择最小和最大数字,我想让它重复这个问题,如果我选择的数字是低还是高,我该怎么做 这是我的密码:Python 如果输出为false,则重复输入,python,python-3.6,Python,Python 3.6,我想用python做一个“猜数字”游戏,我选择最小和最大数字,我想让它重复这个问题,如果我选择的数字是低还是高,我该怎么做 这是我的密码: 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
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")
随机导入
导入时间
打印(“欢迎参加猜谜游戏!”)
时间。睡眠(1)
打印(“选择您的最小数字”)
minnum=输入(“最小:”)
打印(“”)
打印(“选择您的最大数量”)
maxnum=输入(“最大:”)
打印(“”)
打印(“输入您的号码”)
num=输入(“数字:”)
打印(“”)
q=random.randint(int(minnum),int(maxnum))
def game():
如果int(num)q:
打印(“数字较低”)
如果int(num)==q:
打印(“恭喜!你赢了!”)
打破
游戏()
打印(“”)
打印(“”)
输入(“按回车键退出”)
将输入移动到游戏中()并使其成为循环,如下所示:
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
def game():
尽管如此:
打印(“输入您的号码”)
num=输入(“数字:”)
如果int(num)q:
打印(“数字较低”)
如果int(num)==q:
打印(“恭喜!你赢了!”)
打破
如果用户输入了错误的猜测,您需要一个while
循环来继续,如果正确,循环将通过中断退出:
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")
如果您愿意,此变体将执行附加验证:
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))
循环外不能有break
语句。
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))