Python 3 输入(提示)函数



我知道这可能是一个简单的问题,但我似乎无法弄清楚如何在 for 循环变量之后放置一个字符串

stud_num = int(input("How many students do you have? "))
test_num = int(input("How many test for your module? "))
score = 0
for i in range(stud_num):
    print("******** Student #", i+1, "********")
    for s in range(test_num):
        print("Test number ", end="")
        score1 = float(input(s+1))
        score += score1

我提出问题的示例输出是

测试编号 1 :

但现在我目前的输出来自

print("Test number ", end="") 

score1 = float(input(s+1))是 测试编号 1

我不知道如何将": "放入输入中,因为它给了我一个错误,说它期望一个int但得到一个str

不要在printinput之间拆分提示。只需在input提示符中使用格式字符串:

score1 = float(input("Test number %d: " % (s+1)))

或使用str.format

score1 = float(input("Test number {}: ".format(s+1)))

或新的 f 字符串,仅在 python 3.6 之后才允许:

score1 = float(input(f"Test number {s+1}: "))

最新更新