python - 输入内部的参数
问题描述
这里非常初学者的问题!我正在尝试创建一个要求 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()
解决方案
您不需要将 round 作为参数传递给输入函数,而是在此处进行字符串格式化。
试试下面的代码:
time = float(input("Enter the time for performance %s:" % round))
这会将round的值放在字符串中
提示:round 是 python 关键字,您可以使用任何其他变量名而不是 round
推荐阅读
- machine-learning - 感知器 - 统计一致性
- android - 如何使用其中的可滚动列表视图使整个布局可滚动?
- javascript - 在 javascript 中放什么才能知道我输入了一个数字?
- python - 我将如何将数字写在不同的行上?
- python - 在 R 中是否有与 Python 中的 None 语句等效的语句?
- node.js - 快速路由和 jsonwebtoken,创建令牌后保持登录状态
- spring - Redis Spring会话bean未更新
- c# - 在 ASP.NET Core 5 Blazor Web 程序集中添加 NuGet 包的位置?
- python-3.x - 使用 ImageAi 训练自定义数据时出错
- c# - 给定角度的 3D 球体上的随机旋转