Python While循环2 int值
我试图写一个函数,从用户那里得到2个int值,直到它们的和为21,很简单 其目的是不断提示用户传递2个int值,直到满足条件为止。我不确定代码在哪里中断,因为它在满足任一条件时停止,无论是真是假Python While循环2 int值,python,while-loop,Python,While Loop,我试图写一个函数,从用户那里得到2个int值,直到它们的和为21,很简单 其目的是不断提示用户传递2个int值,直到满足条件为止。我不确定代码在哪里中断,因为它在满足任一条件时停止,无论是真是假 def check_for_21(n1,n2): 结果=0 尽管如此: 而结果呢!=21: 尝试: n1=int(输入(“输入第一个数字>>”) n2=int(输入(“输入第二个数字>>”) 如果n1+n2!=21: 打印(“您没有达到21!”) 其他: 打印(“明白了!”) 除: 如果n1+n2==2
def check_for_21(n1,n2):
结果=0
尽管如此:
而结果呢!=21:
尝试:
n1=int(输入(“输入第一个数字>>”)
n2=int(输入(“输入第二个数字>>”)
如果n1+n2!=21:
打印(“您没有达到21!”)
其他:
打印(“明白了!”)
除:
如果n1+n2==21:
打印(“明白了!”)
其他:
打破
打破
这就是您要查找的内容您有多个逻辑错误!然而,这个想法存在,但格式错误。在这个程序中,它连续运行,直到您输入两个数字,它们的总和为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()
这就是你要找的你有多个逻辑错误!然而,这个想法存在,但格式错误。在这个程序中,它连续运行,直到您输入两个数字,它们的总和为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()
获取输入,检查结果,如果得到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:
pass
获取输入,检查结果,如果得到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:
pass
试试这个。这将符合你的要求
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! ")
a=5
而(a试试这个。这会满足你的要求
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! ")
a=5
而(a以下是为我工作的代码的测试结果:
下面是为我工作的代码的测试结果:
你得到答案了吗?你得到答案了吗?