python - 在循环中获取输入有什么区别?
问题描述
我在 python 中运行代码,想知道在循环中获取输入有什么区别。
例如;
restart = ('y')
chances = 3
balance = 1000
while chances >= 0 :
pin = int(input('Please enter your 4 digit pin \n'))
if pin == (1234):
print('You have entered the correct pin')
while restart not in ('N', 'n'):
print ('Press 1 to check balance')
print ('Press 2 to withdraw')
option = int(input('Can you please enter your option \n'))
if option == 1:
print('Your balance is', balance, '\n')
restart = input('Do you like to go back?')
if restart in ('n', 'N'):
print ('Thanks')
break
在上面的代码中,当我
pin = int(input('Please enter your 4 digit pin \n'))
在 while 循环之外时
if restart in ('n', 'N'):
,这个 if 语句不起作用。它开始打印
print('You have entered the correct pin')
而没有终止。
有人可以解释为什么吗?
解决方案
“break”让你脱离了内部的 while 循环while restart not in ('N', 'n'):
。
由于“重新启动”的值将是 'n' \ 'N',因此您将永远不会再进入此循环,但是外部循环while chances >= 0 :
会继续进行,因为您从未更改过机会的值。
推荐阅读
- python - 成功 url 在我基于类的 DeleteView 中不起作用
- python - Python Regex 检测字母之间的下划线
- php - 验证单个产品页面中“添加到购物车”上方添加的自定义字段
- javascript - 使用 Google API 自动完成用户地址字段
- python - 在 Mongoengine 中填充,参考字段
- github - GitHub Action - 从分支 A 推送到分支 B 时触发,但不分支到分支 B
- sql - 根据时间值折叠多行
- c# - 对 C# 中的返回方法和 void 感到困惑
- laravel - 如何为 spatie/laravel-permission 合同/权限制作包装器?
- redux - 在执行第二个 Saga 动作之前执行第一个 Saga 的所有动作