gpt4 book ai didi

python - 当条件满足时,While 循环不会终止

转载 作者:太空宇宙 更新时间:2023-11-03 19:59:46 25 4
gpt4 key购买 nike

在使用 Python 编程时,我陷入了这样一种情况:即使在满足条件之后,while 循环也没有终止

代码如下:

print('--- Alex\'s Calculator ---')
print('1. ADDition')
print('2. SUBstraction')
print('3. MULtiply')
print('4. DIVide')
print('5. EXIT')
x = int(input())

command = ' Enter Your Two numbers To Perform The Operation : '
def ini():
a = int(input())
b = int(input())
return a, b
def resultoo():
result = ' Your Result after Performing The Operation from {} and {} is {}'
print(result.format(a,b,c))
print(' Want To Continue If Yes then Enter Your Choice else Press any number exept 1 - 4')
x = int(input())

while x < 5:
if x == 1:
print(command)
a, b = ini()
c = a + b
resultoo()
elif x < 5:
break

最佳答案

正如 kuro 在评论中指定的那样,您的 while 循环看不到 x,因为它是 resultoo() 的本地变量。

要轻松解决它,只需添加:

return x

在 resultoo() 的末尾

x = resultoo()

在 while 循环中

关于python - 当条件满足时,While 循环不会终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59326345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com