python - How to print "yes" when written "y" in "Are you sure for exit" question?
问题描述
I want to print "yes" when the user writes "y" and print"no" when the user writes "n" in "Are you sure for the exit" question. And the second problem is; if I write any letter instead of "y" or "n", the code is still running. How to fix it?
residuary = 1000
while True:
operation = input("Select operation: ")
if(operation == "q"):
print("Are you sure for exit? (y/n)")
answer = input("Answer:")
y = "yes"
n = "no"
if(answer == "y"):
print("See you again ")
break
else:
continue
elif(operation== "1"):
print("Residuary is ${} .".format(residuary))
elif (operation== "2"):
amount = int(input("Amount you want to invest: "))
residuary += amount
print("${} sent to account.".format(amount))
print("Available Residuary ${} ".format(residuary))
elif (operation == "3"):
amount = int(input("Amount you want to withdraw: "))
if(amount > residuary):
print("You can not withdraw more than available residuary!")
continue
residuary -= amount
print("${} taken from account.".format(amount))
print("Available Resiaduary ${} ".format(residuary))
else:
print("Invalid Operation!")
解决方案
你的问题不是很清楚。你说我想在用户写“y”时打印“yes”,当用户在“你确定退出”问题中写“n”时打印“no”。但是当您使用 input("Answer:") 语句收集用户希望时,这一行就会打印出来。
您是否追求以下代码片段?
if(operation == "q"):
quit = False
while(True):
print("Are you sure you want to exit? ([y]es/[n]o)")
answer = input("Answer:")
if(answer.lower() == 'y': #You may check startswith() as well
quit = True
print('You chose yes')
break
elif(answer.lower() == 'n':
print('You chose no')
break
if quit:
print("See you again ")
break
else:
continue
推荐阅读
- .net - .NET Core 5.0 中的 ModelState 密钥大小写不一致
- java - 代理设置不适用于eclipse,点击eclipse市场时出错
- javascript - Mongoose 不会产生结果,但 mongo shell 会
- math.sqrt - 为什么我得到输出 0.0?包裹丢失或方法有问题?
- python - 我怎么知道用户在 tkinter 中点击了哪个按钮
- html - 如何将扫描仪循环动画旋转到水平
- svg - 无法在 illustrator 中加载的 SVG - 说“此 SVG 无效。打开前验证它。”
- api - Dart 将对象数组发送到 api
- sql - 为数据范围创建具有特定值的表
- javascript - HTML CSS JS 图形可视化