Python:如何提示/输入美国

2024-03-28 22:40:36 发布

您现在位置:Python中文网/ 问答频道 /正文

给出一个数字列表,说:

[0,0,2,4]

我需要提示用户在最小值和最大值之间选择一个数字。你知道吗

例如,我想提示用户:

"Enter a number between 0 and 4: " 

并让用户输入该范围内的数字。为此,我需要计算列表的最小值和最大值。你知道吗

因此,如果列表是[1,2,4,6,7],则提示应更改为:

"Enter a number between 1 and 7: "

我试过这个:

input("Enter a number from {0} and {1}: ").format(min(lst),max(lst))

…但是这不起作用。有人能帮忙吗?你知道吗


Tags: and用户fromformatnumber列表input数字
2条回答

format(...)必须是字符串的方法,而不是输入语句的方法。你的括号错了:

input( "Enter a number from {0} and {1}: ".format(min(lst),max(lst)) )

你的.format必须在input()的括号内。您正在尝试format该函数的结果,而不是字符串。你知道吗

input("Enter a number from {0} and {1}: ".format(min(lst),max(lst)))
                                         ^ Parenthesis moved from here

相关问题 更多 >