Python我需要写一个循环来创建两个独立的运行总数,我不明白

2024-09-29 19:27:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个作业问题,我搞不懂,有人能帮忙吗

--创建一个名为sums的函数,该函数将提示用户输入整数值(正或负)。函数应保持正值和负值的单独运行总计。应允许用户继续输入值,直到输入0停止。在

这是我得到的,但它不起作用

  number = int(raw_input("Enter and a positive or negative integer: "))
  def sums(number):
       while (number > 0):
            posnumber = int(raw_input("Enter another number or 0 to quit: "  ))
            number = number + posnumber
            print "The positive total is", number

       while (number < 0):
            negnumber = int(raw_input("Enter another number or 0 to quit: "  ))
            number = number + negnumber
            print "The negative total is", number

它只是在第一次迭代下运行循环,我不知道该怎么做才能纠正它


Tags: orto函数用户numberinputrawanother
1条回答
网友
1楼 · 发布于 2024-09-29 19:27:05

因为它们是独立的while循环-第一个执行,第二个执行。你根本不想那样。以下是您应该做的布局:

take a number from input, store it as inputnum
while inputnum isn't 0
    determine whether inputnum is positive or negative
    add inputnum to the either posnumber or negnumber
    print the total
    get a new inputnum

也就是说,你应该在一段时间内,而不是两段时间内。在

相关问题 更多 >

    热门问题