首页 > 解决方案 > 输入内部的参数

问题描述

这里非常初学者的问题!我正在尝试创建一个要求 5 个游戏结果的程序,去掉最好和最差的一个,然后计算剩下的结果的平均值 -> 最终结果。

我想我的数学和列表是正确的,但是我找不到在 input 中获取运行数字的方法。(#code 注释中给出的示例。)我试图寻找一种方法来获得它,但不能。Python 说"TypeError: input expected at most 1 argument, got 2"。我理解这意味着我不能将序列号放在输入中?

还有另一种方法来创建它吗?我已经尝试使用函数类型的解决方案来解决它,但无法弄清楚。

def main():

    round = 1

    performance_results = []

    while round < 6:
        time = float(input("Enter the time for performance: ", round))


        # "enter the time for performance 1:
        # "enter the time for performance 2:
        # "enter the time for performance 3: ... till the end of while


        performance_results.append(time)

        round += 1


    # max and min from the list
    max_result = (max(performance_results))
    min_result = (min(performance_results))

    # removes max and min from the list
    performance_results.remove(max_result)
    performance_results.remove(min_result)

    # sum from all the numbers left on the list
    sum = performance_results[0]+performance_results[1]+performance_results[2]

    # average from the results
    final_result = sum / 3

    print("The official competition score is", final_result,"seconds.")


if __name__ == "__main__":
    main()

标签: pythonlistinputarguments

解决方案


您不需要将 round 作为参数传递给输入函数,而是在此处进行字符串格式化。

试试下面的代码:

time = float(input("Enter the time for performance %s:" % round))

这会将round的值放在字符串中

提示:round 是 python 关键字,您可以使用任何其他变量名而不是 round


推荐阅读