python - While 循环停止,输入为空白
问题描述
当 x 输入为空白时,如何使 while 循环停止,而不是在出现“停止”一词时停止?
这就是我所做的,但使用“停止”。但是,如果我将条件更改为 x!=' ',当我尝试将 x 转换为 int 时,它会中断。
x=''
y=''
list=[]
while x!='stop':
x=input('x input: ')
y=int(input('y input: '))
if x!='stop':
list.append((int(x),y))
print('Stop')
print(list)
解决方案
试试这个
x=''
y=''
list=[]
while x!='stop':
x=input('x input: ')
y=int(input('y input: '))
# break loop when x is empty string
if x == '':
break;
if x!='stop':
list.append((int(x),y))
print('Stop')
print(list)
break
如果关键字为空字符串,则中断循环。x
在将变量x
转换为int
.
推荐阅读
- file - TYPO3 如何生成文件的索引,如其 XML 网站导出数据所示?
- r - 为什么 print() 会改变我的函数的输出?
- javafx - 是否可以在我键入时自动格式化 JavaFX HTMLEditor 内容?
- python - Pandas 数据框分配引发 SettingWithCopyWarning
- hsm - CryptokiError:0x60 密钥句柄无效
- cmake - cmake - 如何找出上次构建期间使用的选项
- sapjco3 - sapjco3.dll 已在另一个类加载器中加载
- ios - 无法检查 IOS swift 应用程序中的空文本字段
- android - NavigationView 的 Android ReselectedListener
- sql - 如何检索 MS SQL 中的最新记录?