Reputation: 47
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)
I need this to print out the name and average. When I input '1234' it just goes on normally. Can you help me so it makes it output the else statement? Thanks.
Upvotes: 1
Views: 61
Reputation: 4555
This is one problem:
while (prompt != "1234"):
You're comparing the prompt (which is "Enter your grade...") to the number 1234, and the prompt never changes. Which means it will never be equal to 1234, so it will go on forever! I believe you want to compare to grade1
, rather than prompt
.
Another issue, is the else
statement:
else:
print (name1.title().strip(), name2.title().strip())
average = (sum(grades) / len(grades))
print (average)
You don't need the else statement, because you want those statements to be executed after the while loops ends, right? Then just put them after the while loop. No need for an if
or if-else
statement!
With those corrections, the code will look similar to this:
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)
Only a few syntax errors were holding you back, so keep up the good work!
Upvotes: 1