The Question
Write a program that asks the user to enter their name and grades for all the courses they took this semester. Check that the name is composed of only letters and that each grade entry is composed of numbers only. When the user has entered all their grades, they may enter -1 to indicate they are done. Calculate the average grade and display that to the user.
Sample given to me
Enter your name: Sar@
Please enter a valid name.
Enter your name: sara
Enter your grade for course 1: 90
Enter your grade for course 2: 90s
Please enter a valid grade.
Enter your grade for course 2: 80
Enter your grade for course 3: 70
Enter your grade for course 4: 60
Enter your grade for course 5: -1
Sara, your average grade for 4 courses this semester is 75.0. Well done!
My progress
count=0
sum=0
name = input("Enter your name: ")
while name.isalpha()==False:
print("Please enter a valid name.")
name = input("Enter your name: ")
grade = int(input("Enter your grade for course " str(count 1) ": "))
grade == 1
while grade!=-1:
grade = str(grade)
while grade.isnumeric()==False:
print("Please enter a valid grade.")
grade = input("Enter your grade for course " str(count 1) ": ")
grade =int(grade)
count =1
sum =grade
grade = int(input("Enter your grade for course " str(count 1) ": "))
avg = sum/count
if avg>60:
print(name.capitalize(),", your average grade for",count,"courses this semester is",avg,". Well done!")
else:
print(name.capitalize(),", your average grade for",count,"courses this semester is",avg,". Please do better.")
I get an int error. though I know why I get the error but have no other way to solve this problem. Please help!
CodePudding user response:
You can use try
except
Like this:
count=0
sum=0
name = input("Enter your name: ")
while name.isalpha()==False:
print("Please enter a valid name.")
name = input("Enter your name: ")
grade = 1
while grade!=-1:
try:
grade = int(input("Enter your grade for course " str(count 1) ": "))
if grade == -1: break
count =1
sum =grade
except:
print("Please enter a valid grade.")
avg = sum/count
if avg>60:
print(name.capitalize(),", your average grade for",count,"courses this semester is",avg,". Well done!")
else:
print(name.capitalize(),", your average grade for",count,"courses this semester is",avg,". Please do better.")
CodePudding user response:
I assume you got an int error after entering a non-numeric value as your grade. You convert the grade to an int when you define it without checking if it's a numeric value or not. This will raise an exception, you can avoid it by using try/except
or first checking if grade
is numeric.
CodePudding user response:
Your while grade.isnumeric()==False:
is failing when user enters nothing. It passes through an empty string and in line
grade = int(input("Enter your grade for course " str(count 1) ": "))
it tries to evaluate it as int although it can't since its an empty string.
CodePudding user response:
You have several things wrong with this homework assignment.
Here are some suggestions:
- Append to a list
grades =[]
rather than a running grade that is initialized with a '1'. You don't want the '1' to be part of your average. - Use a try/except clause to validate the user input.
- Compute the average on the list after you exit the while loop.