初学者程序员在这里。我试图编写一个程序,要求用户输入测验成绩,直到他们输入空白输入。此外,我试图让输入从显示" quiz1:"每次用户输入新的测验成绩时,到"测验2:","测验3:"等等。像这样:
quiz1: 10
quiz2: 11
quiz3: 12
这是我到目前为止所写的内容:
grade = input ("quiz1: ")
count = 0
while grade != "" :
count += 1
grade = input ("quiz ", count, ": ")
当输入中输入空白值时,我已成功设法使程序结束,但当我尝试输入测验等级的整数时,我收到以下错误:
Traceback (most recent call last):
File "C:\Users\Kyle\Desktop\test script.py", line 5, in <module>
grade = input ("quiz ", count, ": ")
TypeError: input expected at most 1 arguments, got 3
如何在与成绩输入关联的括号内包含多个参数?
答案 0 :(得分:1)
input
函数只接受一个参数,即消息。然而,为了解决这个问题,一个选择是使用之前的print语句,如下所示:
.
.
.
while grade != "":
count += 1
print("quiz ", count,": ", end="")
grade = input()
答案 1 :(得分:1)
使用.format
,例如:
count = 0
while grade != "" :
count += 1
grade = input('quiz {}:'.format(count))
答案 2 :(得分:0)
当然,很容易在str()函数中创建变量以将其从数字转换为字符串,并使用“ +”分隔所有参数,而在使用“”时以编程方式不要使用“-” +“ Python将被视为一个字符串
grade = input ("quiz 1: ")
count = 1
while grade != "" :
count += 1
grade = input ("quiz "+ str(count) + ": ")