Python 3.x:在一行中进行“打印”和“输入”的最简单方法是什么?

时间:2019-03-31 14:22:58

标签: python-3.x

我是python的新手,循环时总是遇到此问题。我应该怎么做才能使“打印”和“输入”合而为一?

请帮助我^。^ 这是我的代码

subjects = int(input("How many subjects do you have?"))
subject_grades = []
sum = 0
for e in range(0, subjects):
    print("Enter the grade of your subject number ", e+1, ": ", sep="")
    grade = int(input())
    subject_grades.append(grade)
    sum+=grade
average= sum/len(subject_grades)
print("The sum of your grades is", sum, "and the average is", average)

我希望这段代码的输出为:

How many subjects do you have?3
Enter the grade of your subject number 1: 1
Enter the grade of your subject number 2: 2
Enter the grade of your subject number 3: 3
The sum of your grades is 6 and the average is 2.0

但实际输出是:

How many subjects do you have?3
Enter the grade of your subject number 1: 
1
Enter the grade of your subject number 2: 
2
Enter the grade of your subject number 3: 
3
The sum of your grades is 6 and the average is 2.0

1 个答案:

答案 0 :(得分:0)

使用

grade = int(input("Enter the grade of your subject number " + str(e+1) + ": "))

代替

print("Enter the grade of your subject number ", e+1, ": ", sep="")
grade = int(input())