python - while循环中的重复行
问题描述
print('welcome to cinema')
def msg():
exit = input(' if you want to exit press quit')
print()
while True:
age = input(' enter your age ')
if age == 'quit':
break
age = int(age)
if age < 3:
print('your ticket is free')
msg()
elif age < 10:
print('your ticket is S10')
msg()
else:
print('your ticket is S20')
msg()
output:welcome to cinema
enter your age 12
your ticket is S20
if you want to exit press quit
enter your age( why this line is repeated)
我想在输入退出时退出....当我第一次输入退出时它工作正常。但是当我输入年龄值时该行重复
解决方案
试试这个:
import sys
print('welcome to cinema')
def msg():
exit = input('if you want to exit press quit')
print()
while True:
age = input(' enter your age ')
if age == 'quit':
break
age = int(age)
if age < 3:
print('your ticket is free')
elif age < 10:
print('your ticket is S10')
else:
print('your ticket is S20')
exit = input("do you want to quit (y/n): ")
if exit in ["y", "yes"]:
sys.exit('stopped')
推荐阅读
- android - 无法通过按 Android 设备的后退键按钮关闭 Admob 插页式广告
- android - 间歇性 SSLHandshakeException 使用 OKHttp 调用 GET
- html - 为什么我的 ::before 样式显示在 ::after 位置?
- cross-domain - 跨域跟踪,我的 google 跟踪代码管理器 [装饰表单 = True] 它不起作用
- arrays - DataFrame 列(数组类型)包含 Null 值和空数组(len =0)。如何将 Null 转换为空数组?
- lua - Lua - 接受多个输入
- android - 根据其他视图的属性启用视图 - android数据绑定
- python - Python统一随机数生成为三角形
- visual-studio-code - C/C++ 代码导航在 vscode 远程开发中不起作用
- sql - SQL select & group-by 作为 DAX