我正在尝试编写一个函数,从用户那里获取2个int值,直到它们的总和是21,很简单!
其目的是不断提示用户传递2个int值,直到满足条件为止。我不确定代码在哪里中断,因为它在满足任何一个条件时都会停止,无论是true还是false。
def check_for_21(n1,n2):
result = 0
while True:
while result != 21:
try:
n1 = int(input("Enter first number >> "))
n2 = int(input("Enter second number >> "))
if n1+n2 != 21:
print("You did not get to 21! ")
else:
print("You got it! ")
except:
if n1+n2 == 21:
print("You got it! ")
else:
break
break发布于 2019-12-19 19:15:38
这就是你要找的,你有多个逻辑错误!然而,这个想法是存在的,但格式错误。在这个程序中,它会一直运行,直到您输入两个数字,它们的总和是21
def check_for_21():
while True:
n1 = int(input("Enter first number >> "))
n2 = int(input("Enter second number >> "))
if n1+n2 != 21:
print("You did not get to 21! ")
else:
print("You got it! ")
break
check_for_21()发布于 2019-12-19 19:29:05
尝尝这个。这将满足您的要求。
a = 5
while (a<6):
n1 = int(input("Enter first number >> "))
n2 = int(input("Enter second number >> "))
if n1+n2 == 21:
print("You got it ")
break
else:
print("You did not get to 21! ")发布于 2019-12-19 19:17:01
接受输入,检查结果,如果得到21,则中断循环。
def check_for_21(n1,n2):
result = 0
while True:
try:
n1 = int(input("Enter first number >> "))
n2 = int(input("Enter second number >> "))
if n1+n2 != 21:
print("You did not get to 21! ")
else:
print("You got it! ")
break
except:
passhttps://stackoverflow.com/questions/59408405
复制相似问题