为什么不能退出循环

aprilhorse 2011-07-05 08:35:37

i=1
while i:
print("good")
if input("input:")=='q':#我希望能在按下q键的时候退出循环
break




我用python3.2,vista下用python 1.py在控制台输出,发现不能退出循环,请高人指点
...全文
325 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
ralfsumahe 2012-12-01
  • 打赏
  • 举报
回复
debug发现应该写入'q/r'

i=1while i:     
 print("good")     
 if input("input:")=='q/r':#我希望能在按下q键的时候退出循环         
  break 
ralfsumahe 2012-12-01
  • 打赏
  • 举报
回复
debug发现应该写入'q/r' 可能是输入q回车,会带一个/r。
阿小信 2011-11-08
  • 打赏
  • 举报
回复
python3x貌似么有raw_input一说吧
静远 2011-11-07
  • 打赏
  • 举报
回复
话说,3.X之后不是raw_input已经改为input函数了吗?已经没有raw_input函数了
fibbery 2011-11-04
  • 打赏
  • 举报
回复
代码似乎没问题
雷中听风 2011-11-04
  • 打赏
  • 举报
回复
经验证,python2.7需要用raw_input,用input需要输入'q'三个字符,但python3.2不需要,按回车可以中断。同时python3.2的print需要加(),否则报错。如果需要不回车的,看下面代码:

#!/usr/bin/env python2.7
import os,sys
os.system('stty raw')
i=1
while i:
print "good",
print "input:"
a=sys.stdin.read(1)
if a=='q':
os.system('stty -raw')
break
雷中听风 2011-11-04
  • 打赏
  • 举报
回复
同意是raw_input和input的问题。
caihongjftt 2011-11-04
  • 打赏
  • 举报
回复
可能是输入的字符是带回车换行符的,比如键盘按 p,最后得到的数据是 p\n.
应该要将末尾的\n删除。(perl中是使用chorm函数)
livesguan 2011-11-04
  • 打赏
  • 举报
回复
只是input raw_input的问题.
jiaweiqq123 2011-11-03
  • 打赏
  • 举报
回复
i=1
while i:
print("good")
if input("input:")=='q':#我希望能在按下q键的时候退出循环
break
应该为:
i=1
while i:
print("good")
if input("input:")=='q':#我希望能在按下q键的时候退出循环
break
tinym87 2011-10-31
  • 打赏
  • 举报
回复
input("input:")
相当于
eval(raw_input("input:"))

也就是说input()这个函数把你输入的值当作一个python程序表达式,而不是一个字符串,也正是因为这个,当你加上引号"q"输入时,这个输入的值才是真正的字符串,如果想要得到你预期的结果,可以使用下面的代码:
i=1
while i:
print("good")
if raw_input("input:")=='q':
break

这个代码楼上已经给出过了

另外使用一般情况下也不推荐使用input(),这可能造成恶意的代码注入
SChivas 2011-10-31
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 qaz703016035 的回复:]
引用 8 楼 aidings 的回复:
Python code

i=1
while i:
print("good")
if raw_input("input:")== 'q':
break



如果用if input("input:")=='q', 则需键盘输入 ‘q’ 包括引号


正解,input必须加引号才能接受,而且输出为"q"(带引号)
使用raw_in……
[/Quote]

晕,错了,两个输出都是字符q,长度为1
SChivas 2011-10-31
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 aidings 的回复:]
Python code

i=1
while i:
print("good")
if raw_input("input:")== 'q':
break



如果用if input("input:")=='q', 则需键盘输入 ‘q’ 包括引号
[/Quote]

正解,input必须加引号才能接受,而且输出为"q"(带引号)
使用raw_input可以直接输入,输出为q(不带引号)

worldlight 2011-10-31
  • 打赏
  • 举报
回复
就是input和raw_input的问题,可以自己做实验嘛
aidings 2011-10-13
  • 打赏
  • 举报
回复
i=1
while i:
print("good")
if raw_input("input:")== 'q':
break


如果用if input("input:")=='q', 则需键盘输入 ‘q’ 包括引号
王晓彤 2011-10-13
  • 打赏
  • 举报
回复

i=1;
while i:
print("Good");
if input("Input:")=='q':
break;


Python3.2代码没有问题,楼主的break需要缩进。
Daanlex 2011-10-13
  • 打赏
  • 举报
回复


while True:
print('good')
if str(raw_input('input')) == 'q':
break

Bobeerat 2011-07-18
  • 打赏
  • 举报
回复

判断条件是输入和字符‘q’比较
用户输入是字符串,非数值
但是input()取的是输入的实际意义的值,非字符串
如果条件是输入与变量q比较,q不应有双引号
若是作字符串判断
因该用raw_input()
limenghun 2011-07-17
  • 打赏
  • 举报
回复
注意缩进,if input("input:")=='q':
break
把这个错误改过来后,
i=1
while i:
print("good")
if input('input: ')=='q':
break
保存为 test.py
$ python3 test.py
good
input: q (记得在按下Q后再按回车)
$
BoredNight 2011-07-11
  • 打赏
  • 举报
回复
没按回车的吧程序会一直等待的吧?
感觉是不是input 和 raw input的区别?
If you use input, then the data you type is is interpreted as a Python Expression which means that you end up with gawd knows what type of object in your target variable, and a heck of a wide range of exceptions that can be generated. So you should NOT use input unless you're putting something in for temporary testing, to be used only by someone who knows a bit about Python expressions.

raw_input always returns a string because, heck, that's what you always type in ... but then you can easily convert it to the specific type you want, and catch the specific exceptions that may occur. Hopefully with that explanation, it's a no-brainer to know which you should use.

Example

ccode = 386

first = input("please input something: ")
second = raw_input("please raw_input something: ")

print "First was ... ",first
print "Second was ... ",second

as_an_int_1 = int(first)
as_an_int_2 = int(second)

as_an_int_1 += 7
as_an_int_2 += 9

print "integers with addition ... ",as_an_int_1,as_an_int_2

Here's an example of that running ...

Dorothy:dec07 grahamellis$ python ivr
please input something: ccode
please raw_input something: ccode
First was ... 386
Second was ... ccode
加载更多回复(3)

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧