繁体   English   中英

While 循环停止,输入为空白

[英]While Loop stop with blank input

当 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)

如果x为空字符串, break关键字会中断循环 在将变量x强制转换为int之前验证它。

这应该做你想做的事。

x='x'  
y='y'  
list=[]  
while x!='':  
    x=input('x input: ')  
    if x!='':  
        y=int(input('y input: '))  
        list.append((int(x),y))  
    
print('Stop')  
print(list)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM