name1 = input("What is your first name: ")
name2 = input("What is your last name: ")
grades = []
prompt = "Enter your grade or enter '1234' to get your letter grade and average: "
print('Please enter your grades:')
grade1 = input(prompt).strip()
while (prompt != 1234):
grades.append(grade1)
grade1 = input(prompt).strip()
else:
print (name1.title().strip(), name2.title().strip())
average = (sum(grades) / len(grades))
print (average)
我需要这个来打印出名字和平均值。当我输入' 1234'它只是正常进行。你可以帮助我,所以它输出else语句吗?感谢。
答案 0 :(得分:1)
这是一个问题:
grade1
您将提示(即“输入您的成绩...”)与数字1234进行比较,提示永远不会更改。这意味着永远不会等于1234,所以它会永远持续下去!我认为您希望与prompt
进行比较,而不是else
。
另一个问题是else:
print (name1.title().strip(), name2.title().strip())
average = (sum(grades) / len(grades))
print (average)
声明:
if
你不需要else语句,因为你希望在while循环结束后执行这些语句,对吧?然后将它们放在while循环之后。无需if-else
或name1 = input("What is your first name: ")
name2 = input("What is your last name: ")
grades = []
prompt = "Enter your grade or enter '1234' to get your letter grade and average: "
print('Please enter your grades:')
grade1 = input(prompt).strip()
while (grade1 != "1234"):
grades.append(grade1)
grade1 = input(prompt).strip()
print (name1.title().strip(), name2.title().strip())
average = (sum(grades) / len(grades))
print (average)
声明!
通过这些更正,代码看起来与此类似:
{{1}}
只有少数语法错误阻碍了你,所以保持良好的工作!