嗨,我对python和一般编程都很陌生。我正在尝试编写一个程序,它使用while循环将整数从1添加到输入的数字。如果用户输入0或负数,程序还必须给出错误声明。到目前为止,整数加起来,错误语句工作,但程序不是循环,它只要求用户输入一个数字一次。请帮帮忙。到目前为止这是我的源代码。谢谢
x = int(input("Enter a positive number not including zero:" ))
total = 0
n = 1
while n <= x:
total = total + n
n = n + 1
# prints the total of integers up to number entered
print("Sum of integers from 1 to number entered= ",total)
if x <= 0 or x == -x:
print ("invalid entry")发布于 2014-06-24 03:47:59
试试这个密码..。
op='y'
while op=='y':
x = int(input("Enter a positive number not including zero:" ))
total = 0
n = 1
if x > 0:
while n <= x:
total = total + n
n = n + 1
# prints the total of integers up to number entered
print("Sum of integers from 1 to number entered= ",total)
else:
print ("invalid entry")
op = raw_input("Are you want to continue this operation (y/n):" )发布于 2014-06-24 03:17:42
把你的全部代码往这边放
done = False
while not done:
//your entire code here except the last 2 lines
if x > 0:
done = Truehttps://stackoverflow.com/questions/24377703
复制相似问题