繁体   English   中英

数和演习,Python 3

[英]Sum of Numbers Exercise, Python 3

任何帮助将非常感激。 我是新学习Python。 这是我必须完成的练习。 我已经认真地花了大约3到4个小时的时间,因此不必担心我“正在请别人为我工作”。 这是练习以及到目前为止的内容。 我一直在困惑这些,并在学习过程中“破解”了自己的方式。 如果您知道整个代码并且不介意包含它,我将非常感谢。 练习:编写一个带有循环的程序,要求用户输入正数。 用户应输入一个负数来表示序列结束。 当用户结束程序时(键入负数),应显示正数之和。 这是我到目前为止所拥有的。

# The main function.
def main():
# Variable to control the outer loop.
another = 'y'
while another == 'y' or another == 'Y':
    numbers()
    another = input('Run this program again?\n\
Enter y for yes.')
def numbers():
positive = int(input('Enter a positive number: '))
while positive > 0:
positive = int(input('Enter a positive number, or enter a\n\
negative number to end and calculate the sum: '))
positive = int(input('Enter a positive number: '))
while positive < 0:
for i in range (positive):
    print(i)
# Call the main function.
main()

关于什么:

# The main function.
def main():
  mysum = 0
  while True:
    positive = int(input('Enter a positive number, or enter a\n\
negative number to end and calculate the sum: '))
    if positive < 0:
      print mysum
      return
    mysum = mysum + positive

# Call the main function. 
main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM