医生程序python

2024-09-27 17:49:51 发布

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

下面是一个简短的医生计划,我正在做,这是一个开始,不幸的是,它不起作用。这是我收到的错误- TypeError:输入最多应有1个参数,但得到4个

least = 0
most = 100

while True:
    try:
        levelofpain = int(input("How much is it hurting on a scale of", (least), "to", (most)))
        while llevelofpain < least or levelofpain > most:
            print ("Please enter a number from", (least), "to", (most))
            levelofpain = int(input("How much is it hurting on a scale of", (least), "to", (most)))
        break
    except ValueError:
        print ("Please enter a number from", (least), "to", (most))

提前谢谢!在

p.s.使用Python3.3


Tags: oftomostinputisonithow
2条回答

错误消息是不言自明的:您正在向input(...)传递四个参数,而它只接受一个参数。在

解决方法是将参数转换为单个字符串。在

levelofpain = int(input(
    "How much is it hurting on a scale of {} to {}? ".format(least, most)))

对于格式化字符串,您可能需要使用Python的.format()函数。看看这个问题:String Formatting in Python 3

在Python中格式化字符串有两种主要方法,一种是使用str类的.format方法,另一种是使用%符号的旧C风格方法:

str.格式():

"This is a string with the numbers {} and {} and {0}".format(1, 2)

C样式格式(%):

^{pr2}$

我强烈建议不要使用C风格的格式,而是使用现代函数版本。另一种我不推荐的方法是用您自己的定义替换输入函数:

old_input = input
def input(*args):
    s = ''.join([str(a) for a in args])
    return old_input(s)

input("This", "is", 1, 2, 3, "a test: ")

相关问题 更多 >

    热门问题