首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python编程Loop循环

问Python编程Loop循环
EN

Stack Overflow用户
提问于 2014-06-24 03:04:21
回答 2查看 113关注 0票数 0

嗨,我对python和一般编程都很陌生。我正在尝试编写一个程序,它使用while循环将整数从1添加到输入的数字。如果用户输入0或负数,程序还必须给出错误声明。到目前为止,整数加起来,错误语句工作,但程序不是循环,它只要求用户输入一个数字一次。请帮帮忙。到目前为止这是我的源代码。谢谢

代码语言:javascript
复制
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")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-24 03:47:59

试试这个密码..。

代码语言:javascript
复制
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):" )
票数 0
EN

Stack Overflow用户

发布于 2014-06-24 03:17:42

把你的全部代码往这边放

代码语言:javascript
复制
    done = False
    while not done:
        //your entire code here except the last 2 lines
        if x > 0:
            done = True
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24377703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档