Python 当条件满足时,While循环不终止
在用Python编程时,我遇到了这样一种情况,即Python 当条件满足时,While循环不终止,python,python-3.x,Python,Python 3.x,在用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 Per
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
print('--Alex的计算器--')
打印('1.添加')
打印('2.减法')
打印('3.乘法')
打印('4.除')
打印('5.退出')
x=int(输入())
command='输入两个数字以执行操作:'
def ini():
a=int(输入())
b=int(输入())
返回a,b
def resulto():
结果='从{}和{}执行操作后的结果是{}'
打印(结果格式(a、b、c))
打印('要继续,如果是,则输入您的选择,否则按除1-4'以外的任何数字)
x=int(输入())
当x<5时:
如果x==1:
打印(命令)
a、 b=ini()
c=a+b
结果太()
elif x<5:
打破
您可以使用全局
变量来更改此:
def resultoo():
结果='从{}和{}执行操作后的结果是{}'
打印(结果格式(a、b、c))
打印('要继续,如果是,则输入您的选择,否则按除1-4'以外的任何数字)
x=int(输入())
进入:
解释:
x
是一个全局参数,它与函数闭包外的参数相同,但不在函数内,函数有自己的参数,因此如果要更改在函数外初始化的全局参数,则需要先调用global
语句,这将使x
成为全局x
您可以使用global
var将其更改为:
def resultoo():
结果='从{}和{}执行操作后的结果是{}'
打印(结果格式(a、b、c))
打印('要继续,如果是,则输入您的选择,否则按除1-4'以外的任何数字)
x=int(输入())
进入:
解释:
x
是一个全局参数,它与函数闭包外的参数相同,但不在函数内,函数有自己的参数,因此如果要更改在函数外初始化的全局参数,则需要先调用global
语句,这将使x
成为注释中指定的全局x,而while循环无法看到x,因为它是resulttoo()的本地对象
要轻松解决此问题,只需添加:
return x
在第()段末尾
及
在您的while循环中如注释中所指定的kuro,您的while循环无法看到x,因为它是resulttoo()的本地对象
要轻松解决此问题,只需添加:
return x
在第()段末尾
及
在while循环中当输入选项5时,您希望退出
我补充说
改变
elif x < 5:
并补充说
sys.exit(0)
我还添加了getMenu()函数
这是在我的编辑器中运行的完整代码:
import sys
def ini():
command = ' Enter Your Two numbers To Perform The Operation : '
print(command)
a = int(input())
b = int(input())
return a, b
def resultoo(a, b, c):
result = ' Your Result after Performing The Operation from {} and {} is {}'
print(result.format(a, b, c))
def getMenu(x):
if x == 0:
print("Choose menu item")
x = int(input())
elif x != 0:
print(' Want To Continue If Yes then Enter Your Choice else Press any number exept 1 - 4')
x = int(input())
return x
def main():
x = 0
while x < 5:
print('\n\n1. ADDition')
print('2. SUBstraction')
print('3. MULtiply')
print('4. DIVide')
print('5. EXIT\n')
x = getMenu(x)
if x == 1:
a, b = ini()
c = a + b
resultoo(a, b, c)
elif x == 5:
sys.exit(0)
else:
print("No valid menu item")
if __name__ == '__main__':
print('----------------------------------------------------------------------------------------------------------')
print('-------------------------------------------- Alex\'s Calculator -------------------------------------------')
main()
导入系统
def ini():
command='输入两个数字以执行操作:'
打印(命令)
a=int(输入())
b=int(输入())
返回a,b
定义结果(a、b、c):
结果='从{}和{}执行操作后的结果是{}'
打印(结果格式(a、b、c))
def getMenu(x):
如果x==0:
打印(“选择菜单项”)
x=int(输入())
以利夫x!=0:
打印('要继续,如果是,则输入您的选择,否则按除1-4'以外的任何数字)
x=int(输入())
返回x
def main():
x=0
当x<5时:
打印('\n\n1.添加')
打印('2.减法')
打印('3.乘法')
打印('4.除')
打印('5.退出\n')
x=获取菜单(x)
如果x==1:
a、 b=ini()
c=a+b
结果(a、b、c)
elif x==5:
系统出口(0)
其他:
打印(“无有效菜单项”)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
打印(“------------------------------------------------------------------------------------------------------------------”)
打印(“-------------------------------------------------------------亚历克斯的计算器------------------------------------------”)
main()
我还格式化了您的代码(alt+Enter-in-Pycharm)以符合PEP8标准;) 输入选项5后,您希望退出
我补充说 改变
elif x < 5:
并补充说
sys.exit(0)
我还添加了getMenu()函数
这是在我的编辑器中运行的完整代码:
import sys
def ini():
command = ' Enter Your Two numbers To Perform The Operation : '
print(command)
a = int(input())
b = int(input())
return a, b
def resultoo(a, b, c):
result = ' Your Result after Performing The Operation from {} and {} is {}'
print(result.format(a, b, c))
def getMenu(x):
if x == 0:
print("Choose menu item")
x = int(input())
elif x != 0:
print(' Want To Continue If Yes then Enter Your Choice else Press any number exept 1 - 4')
x = int(input())
return x
def main():
x = 0
while x < 5:
print('\n\n1. ADDition')
print('2. SUBstraction')
print('3. MULtiply')
print('4. DIVide')
print('5. EXIT\n')
x = getMenu(x)
if x == 1:
a, b = ini()
c = a + b
resultoo(a, b, c)
elif x == 5:
sys.exit(0)
else:
print("No valid menu item")
if __name__ == '__main__':
print('----------------------------------------------------------------------------------------------------------')
print('-------------------------------------------- Alex\'s Calculator -------------------------------------------')
main()
导入系统
def ini():
command='输入两个数字以执行操作:'
打印(命令)
a=int(输入())
b=int(输入())
返回a,b
定义结果(a、b、c):
结果='从{}和{}执行操作后的结果是{}'
打印(结果格式(a、b、c))
def getMenu(x):
如果x==0:
打印(“选择菜单项”)
x=int(输入())
以利夫x!=0:
打印('要继续,如果是,则输入您的选择,否则按除1-4'以外的任何数字)
x=int(输入())
返回x
def main():
x=0
当x<5时:
打印('\n\n1.添加')
打印('2.减法')
打印('3.乘法')
打印('4.除')
打印('5.退出\n')
x=获取菜单(x)
如果x==1:
a、 b=ini()
c=a+b
结果(a、b、c)
elif x==5:
系统出口(0)
其他:
打印(“无有效菜单项”)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
打印(“------------------------------------------------------------------------------------------------------------------”)
打印(“-------------------------------------------------------------亚历克斯的计算器------------------------------------------”)
main()
我还格式化了您的代码(alt+Enter-in-Pycharm)以符合PEP8标准;)
resultoo()
中的x
是一个局部变量对不起@bad\u编码器我们同时提交了编辑,所以