Python 正整数和、平均数、计数

Python 正整数和、平均数、计数,python,Python,我的编程教授委托我创建一个程序,要求用户输入任意数量的正整数(包括零)。除正整数以外的任何内容都会停止输入并计算/显示数字的总和、平均值和计数 我遇到的主要问题是返回多个正整数以通过程序传递。我似乎找不到我需要的东西 inputPostiveInteger() 他让程序运行的方式就是一个例子- Enter a positive integer, anything else to quit: 1 Enter a positive integer, anything else to quit: 5

我的编程教授委托我创建一个程序,要求用户输入任意数量的正整数(包括零)。除正整数以外的任何内容都会停止输入并计算/显示数字的总和、平均值和计数

我遇到的主要问题是返回多个正整数以通过程序传递。我似乎找不到我需要的东西

inputPostiveInteger()

他让程序运行的方式就是一个例子-

Enter a positive integer, anything else to quit: 1
Enter a positive integer, anything else to quit: 5
Enter a positive integer, anything else to quit: 10
Enter a positive integer, anything else to quit: cat
Sum 16
Average 5.3
Total numbers 3
我尝试了多种try/except和while not子句的方法来获得正确的输入部分,但似乎无法围绕这一部分来思考

我了解如何计算/显示平均数、计数、总和的公式,但似乎无法在多个公式中获得经过审查的整数项

例如,用于尝试输入

def inputPositiveInteger():
    try:
        userInt = int(input("Enter a positive integer, anything else to quit: ")) > -1
    except ValueError:
        return -1

def main():
    total = 0
    count = 0
    posInt = inputPositiveInteger()
    while posInt != -1:
        total += posInt
        count += 1

main()

试试这个:

def finish(summ,count):
    print('Sum',summ)
    print('Count',count)
    print('Avg',round(summ/count,5))

summ = 0
count = 0
while True:
    try:
        userInt = int(input('Enter a positive integer, anything else to quit: '))
        if userInt >= 0:
            summ += userInt
            count += 1 
        else:
            finish(summ,count)
            break
    except:
        finish(summ,count)
        break
试试这个:

def finish(summ,count):
    print('Sum',summ)
    print('Count',count)
    print('Avg',round(summ/count,5))

summ = 0
count = 0
while True:
    try:
        userInt = int(input('Enter a positive integer, anything else to quit: '))
        if userInt >= 0:
            summ += userInt
            count += 1 
        else:
            finish(summ,count)
            break
    except:
        finish(summ,count)
        break

我试图使我的解决方案与您的代码有点相似:

def inputPositiveInteger():
    try:
        userInt = int(input("Enter a positive integer, anything else to quit: "))
    except ValueError:
        return -1

    if userInt < 0:
        return -1
    return userInt

def main():
    total = 0
    count = 0
    posInt = inputPositiveInteger()
    while posInt != -1:
        total += posInt
        count += 1
        posInt = inputPositiveInteger()

    if count == 0:
        print("Sum:", None)
        print("Average:", None)
        print("Total numbers:", None)
    else:
        print("Sum:", total)
        print("Average:", total/count)
        print("Total numbers:", count)

main()

希望这有帮助,祝你的课好运。

我试图让我的解决方案与你的代码有点相似:

def inputPositiveInteger():
    try:
        userInt = int(input("Enter a positive integer, anything else to quit: "))
    except ValueError:
        return -1

    if userInt < 0:
        return -1
    return userInt

def main():
    total = 0
    count = 0
    posInt = inputPositiveInteger()
    while posInt != -1:
        total += posInt
        count += 1
        posInt = inputPositiveInteger()

    if count == 0:
        print("Sum:", None)
        print("Average:", None)
        print("Total numbers:", None)
    else:
        print("Sum:", total)
        print("Average:", total/count)
        print("Total numbers:", count)

main()

希望这有帮助,祝你的课好运。

谢谢你提出的缩小输入范围的建议,最后我将此作为最终提交的代码

def calcAverage(total, count):
    return round(float(total) / float(count), 2)

# prompt user to enter posInt
def inputPositiveInteger():
    userInt = input("Enter a positive integer, anything else to quit: ")
    # return input if digit, else return -1
    if not userInt.isdigit():
        return -1
    return int(userInt)

def main():
    # call function
    posInt = inputPositiveInteger()
    nums = []
    while posInt != -1:
        nums.append(posInt)
        posInt = inputPositiveInteger()

    if nums:
        print("Sum", sum(nums))
        print("Average", calcAverage(sum(nums), len(nums)))
        print("Total numbers", len(nums))


main()

感谢您提出缩小输入范围的建议,我最终将此作为最终提交的代码

def calcAverage(total, count):
    return round(float(total) / float(count), 2)

# prompt user to enter posInt
def inputPositiveInteger():
    userInt = input("Enter a positive integer, anything else to quit: ")
    # return input if digit, else return -1
    if not userInt.isdigit():
        return -1
    return int(userInt)

def main():
    # call function
    posInt = inputPositiveInteger()
    nums = []
    while posInt != -1:
        nums.append(posInt)
        posInt = inputPositiveInteger()

    if nums:
        print("Sum", sum(nums))
        print("Average", calcAverage(sum(nums), len(nums)))
        print("Total numbers", len(nums))


main()

在try块中,您不会遇到错误,因此它永远不会结束,出现值错误的原因包括被零除,我建议您不要尝试在if语句中使用一个简单的中断,没有理由使这样一个简单的任务过于复杂您只是没有从函数返回
userInt
,因此,
posInt
将被设置为
None
(函数隐式返回此值)。如果输入有效,则不会从inputPositiveInteger返回,而while循环是无止境的,或者不会命中,因为输入值在其内部从未更改。您是否以任何形式调试了代码?#在try块中,您不会遇到错误,因此它永远不会结束,出现值错误的原因包括被零除,我建议您不要尝试在if语句中使用一个简单的中断,没有理由使这样一个简单的任务过于复杂您只是没有从函数返回
userInt
,因此,
posInt
将被设置为
None
(函数隐式返回此值)。如果输入有效,则不会从inputPositiveInteger返回,而while循环是无止境的,或者不会命中,因为输入值在其内部从未更改。您是否以任何形式调试了代码?#Up投票赞成尝试复制代码结构,尽管优秀的答案可以解释OP代码的问题所在。Up投票赞成尝试复制代码结构,尽管优秀的答案可以解释OP代码的问题所在。