输入内部的参数
Argument inside of input
这里是非常新手的问题!我正在尝试创建一个程序,要求获得 5 场比赛的成绩,去掉最好的和最差的,然后计算平均数剩下的 -> 最终结果。
我想我的数学和列表是正确的,但是我找不到在输入 [=21] 中获取 运行 数字的方法=]。 (#code 注释中给出的示例。)我试图寻找一种方法来获得它,但没有成功。 Python 说 “TypeError:输入预计最多 1 个参数,得到 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()
这应该对你有帮助:
float(input(f"Enter the time for performance {round}:"))
正如 Amit 提到的,不要使用 round
等关键字作为变量名称。
您不需要将 round 作为参数传递给输入函数,而是在此处进行字符串格式化。
试试下面的代码:
time = float(input("Enter the time for performance %s:" % round))
这会将 round 的值放入字符串中
提示:round 是 python 关键字,您可以使用任何其他变量名代替 round
如果我正确地解释了这个问题;你想根据数字更改它要求的输入,我们可以使用字符串格式来做到这一点!
Python 有几种我们可以格式化字符串的方法(取决于您的 python 版本,您可能会限制为更少)
f-string 格式化
f strings 相当于 js 的 string litterals,提供了一种非常容易阅读的格式,并在字符串的开头用 f
表示,注意:对于 python 3.6+ iirc
a = 123
b = "hello"
my_msg = f"{b} world, have some numbers: {a}"
print(my_msg)
# will give us as a response:
>>> hello world, have some numbers: 123
format() 方法
str.format()
比 f 字符串可读性差,但在某些地方更有用。
他们为 Python 3.x+ 工作并使用 {}
占位符
a = 123
b = hello
my_msg = "{} world, have some numbers: {a}".format(b, a=a) # yes! you can use place holders to be used as kwargs.
print(my_msg)
# will give us as a response:
>>> hello world, have some numbers: 123
还有其他格式化方法,但这些是最流行的,人们已经提到了另一个例子。
这里是非常新手的问题!我正在尝试创建一个程序,要求获得 5 场比赛的成绩,去掉最好的和最差的,然后计算平均数剩下的 -> 最终结果。
我想我的数学和列表是正确的,但是我找不到在输入 [=21] 中获取 运行 数字的方法=]。 (#code 注释中给出的示例。)我试图寻找一种方法来获得它,但没有成功。 Python 说 “TypeError:输入预计最多 1 个参数,得到 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()
这应该对你有帮助:
float(input(f"Enter the time for performance {round}:"))
正如 Amit 提到的,不要使用 round
等关键字作为变量名称。
您不需要将 round 作为参数传递给输入函数,而是在此处进行字符串格式化。
试试下面的代码:
time = float(input("Enter the time for performance %s:" % round))
这会将 round 的值放入字符串中
提示:round 是 python 关键字,您可以使用任何其他变量名代替 round
如果我正确地解释了这个问题;你想根据数字更改它要求的输入,我们可以使用字符串格式来做到这一点!
Python 有几种我们可以格式化字符串的方法(取决于您的 python 版本,您可能会限制为更少)
f-string 格式化
f strings 相当于 js 的 string litterals,提供了一种非常容易阅读的格式,并在字符串的开头用 f
表示,注意:对于 python 3.6+ iirc
a = 123
b = "hello"
my_msg = f"{b} world, have some numbers: {a}"
print(my_msg)
# will give us as a response:
>>> hello world, have some numbers: 123
format() 方法
str.format()
比 f 字符串可读性差,但在某些地方更有用。
他们为 Python 3.x+ 工作并使用 {}
占位符
a = 123
b = hello
my_msg = "{} world, have some numbers: {a}".format(b, a=a) # yes! you can use place holders to be used as kwargs.
print(my_msg)
# will give us as a response:
>>> hello world, have some numbers: 123
还有其他格式化方法,但这些是最流行的,人们已经提到了另一个例子。