只是想写一个程序,接受用户的输入并将其添加到“数字”列表中:
print "Going to test my knowledge here"
print "Enter a number between 1 and 20:"
i = raw_input('>> ')
numbers = []
while 1 <= i <= 20 :
print "Ok adding %d to numbers set: " % i
numbers.append(i)
print "Okay the numbers set is now: " , numbers
但是,当我执行程序时,它只运行到raw_input()
Going to test my knowledge here
Enter a number between 1 and 20:
>>> 4
我有没有遗漏一些基本原则??
raw_input
返回的字符串不是整数:所以
使用
int()
:如果只接受用户的一个输入,请使用
if
:对多个输入使用
while
:要添加到Ashwini的答案中,您会发现原始输入将只运行一次。如果要继续提示用户,请将原始输入放入while循环中:
相关问题 更多 >
编程相关推荐