python 基本回路和总计正输入[闭合]

mec1mxoz  于 2023-02-28  发布在  Python
关注(0)|答案(3)|浏览(157)
    • 已关闭**。此问题需要超过focused。当前不接受答案。
    • 想要改进此问题吗?**更新此问题,使其仅关注editing this post的一个问题。

16天前关闭。
Improve this question
考虑:

print("Welcome to the number program")
number = input("Please give me a number \n")
number = int(number)
total_number = 0
entries = 0

while number > 0:
    total_number = total_number + number
    print(total_number)
    number = input("Please give me another number! \n")
    number = int(number)
    entries = int(entries) + 1
if number < 0:
    print("Sorry, this value needs to be positive. Please enter a
different number.")

if number == -999:
    print(total_number)
    print(entries)
    print(total_number/entries)

我在上编程初学者的课,这本书有时并不是很有帮助。我正试着写一个基本的程序,它接受正数,对它们求和,并在最后求出平均值。它也拒绝负数,并询问是否输入了-999。我打印所有条目的平均值、条目数量和值计数。
程序运行正常,但就是写不出我想要的东西。

liwlm1x9

liwlm1x91#

从你写的东西和代码中的注解来看,我猜你希望程序继续运行,如果你输入了一个非正数,我会把它重写为:

print("Welcome to the number program")
total_number = 0
entries = 0

while True:
    number = input("Please give me a number \n")
    number = int(number)

    if number == -999:
        break

    if number <= 0:
        print("Sorry, this value needs to be positive. Please enter a different number.")
        continue

    total_number = total_number + number
    entries += 1
    print(total_number)

print(total_number)
print(entries)
print(total_number / entries)

此外,还可以使用entries += 1递增数字

5gfr0r5j

5gfr0r5j2#

1.在大多数情况下,您不应该先创建变量,但在这种情况下,您应该先创建number = 0tally = 0total_number = 0
1.接受while循环中的第一个数字,并处理其中的所有逻辑。

  1. while循环应该继续循环,直到满足最后一个条件(似乎是number == -999
    1.如果您输入一个负数,理货应该增加吗?我认为不应该。那么0呢?将tally的增量和total_number的加法包含在if number > -1:条件中。使用if else检查number == -999,使用else处理无效条目。
    1.最后,把print语句移出while循环,它也不需要一个条件,因为现在,如果你退出while循环,这个条件就已经满足了。
    最后一点,这只是一个很好的了解/拥有和纯粹的语法糖,大多数语言支持缩写增量。有一个更好的词来形容它,但要点很简单。
total_number += number
# is exactly the same as 
total_number = total_number + number
# but way nicer to read and write :)
print("Welcome to the number program")
    number = 0
    total_number = 0
    entries = 0

    while number != -999:
        number = input("Please enter a number! \n")

        number = int(number)

        if number >= 0
            total_number += number
            entries += 1
            print("Current sum: " + total_number)
        elif number == -999:
            break
        else
            print("Sorry, this value needs to be positive.")

    
    print("Sum of entries: "+str(total_number))
    print("Number of entries: " + str(entries))
    print("Average entry: " +str(total_number/entries))
cgfeq70w

cgfeq70w3#

我已经重写了你的代码。但是我不确定目标是什么。不管怎样,如果值小于0,循环就会退出,新的值也不会从输入中被接受。还有一些东西我写得更优雅了。

print("Welcome to the number program")
number=int(input("Please give me a number \n"))
total_number=0
entries=0

while number > 0:
    total_number += number
    print(total_number)
    number = int(input("Please give me another number! \n"))
    entries += 1
    if number == -999:
        print(total_number)
        print(entries)
        print(total_number/entries)
        break
    elif number < 0:
        number = input("Sorry, this value needs to be positive. Please enter a different number!")

相关问题