python: grading assignment, negative value - python

I am working on a python lettering assignment.
90 or above is an A and so on and so on for the rest of the letter grades; but when a value is inputted as a negative number, I need the code to do nothing other than display an error.
This is what i tried so far:
#Design a Python program to assign grade to 10 students
#For each student, the program first asks for the user to enter a positive number
#A if the score is greater than or equal to 90
#B if the score is greater than or equal to 80 but less than 90
#C if the score is greater than or equal to 70 but less than 80
#D if the score is greater than or equal to 60 but less than 70
#F is the score is less than 60
#Ihen the program dispalys the letter grade for this student.
#Use while loop to repeat the above grade process for 10 students.
keep_going = 'y'
while keep_going == "y":
num = float(input("Enter a number: "))
if num >= 90:
print("You have an A")
elif num >= 80:
print("You have an 3")
elif num >= 70:
print("You have an C")
elif num >= 60:
print("You have an D")
elif (num < 60 and <= 0:
print ("You have an F")
else:
print("lnvalid Test Score.")
Original screenshot

I see three problems, all in the same line:
elif (num < 60 and <= 0:
Syntax: num < 60 and <= 0 is not a valid expression; should be num < 60 and num <= 0
Logic: num <= 0 is not what you want, it should be num >= 0
Syntax: you missed a closing bracket ).
If you change those, it should work.

grade = int(input("Enter Score:"))
print "FFFFFDCBAA"[grade//10] if grade >= 0 else "ERROR!!!!"

you just have to change your elif for below 60.
keep_going = 'y'
while keep_going == "y":
num = float(input("Enter a number: "))
if num >= 90:
print("You have an A")
elif num >= 80:
print("You have an 3")
elif num >= 70:
print("You have an C")
elif num >= 60:
print("You have an D")
elif 60 > num >= 0:
print ("You have an F")
else:
print("lnvalid Test Score.")

Related

Menu driven program

I'm trying to complete my menu driven program; however, I can't get this code to function properly. the goal is to get information from my previous code then get the evaluated output here. It keeps asking to execute my previous function before the menu pops up. get_list is the previous function that gets the list of numbers from the user.
previous code
def get_list(score_list=None):
if score_list is None:
score_list = []
n = int(input('How many scores do you want to enter? '))
for n in range(1, n + 1):
score = float(input('Enter score #{}: '.format(n)))
while True:
if 0 > score or score > 100:
print('Invalid score entered!!\n', 'Score should be a value between 0 and 100')
score = float(input('Enter score #{}: '.format(n)))
continue
else:
score_list.append(score)
break
return score_list
pass
problem code
def evaluate_list():
score_list = get_list()
lowest_score = min(score_list)
score_list.remove(lowest_score)
score_average = sum(score_list) / len(score_list)
if 90 < score_average <= 100:
grade = 'A'
elif 70 < score_average <= 80:
grade = 'B'
elif 40 < score_average <= 60:
grade = 'C'
elif score_average > 20 and score_average >= 40:
grade = 'D'
else:
grade = 'F'
print('----------Results--------------')
print('Lowest Score : ', lowest_score)
print('Modified List: ', score_list)
print('score Average: ', f'{score_average:.2f}')
print('Grade : ', grade)
evaluate_list()

How do I ask for an input from a user and check that value within a range to give me the correct response

So my professor for python assigned an exercise which was to create a program to accept a user input or "score" and create an if else loop with an if else nested loop inside the first loop to check if the score is greater than or equal to 80. If that's satisfied, print a statement saying "you passed." and if the score is equal to 90 or more, then it should say"you got an A." and if the score is below an 80, it should say "you failed."
so far, this is the code I was able to come up with. And it runs up until the score entered is anything equal to or more than 90.
userScore = int(input('enter score:\n'))
if userScore < 80:
userScore = int(input('you failed. Try again:\n'))
if userScore == 80 or userScore > 80:
print(f'good job, you got a B.')
elif userScore >= 90:
print('you got an A')
else:
print('enter a differnt score')
as per your question your code should be :
score = int(input("Please enter your score: "))
if score >= 80:
print("You passed.")
if score >= 90:
print("You got an A.")
else:
print("You failed.")
while(1):
score = int(input("Please enter your score: "))
if score >= 80:
print("You passed.")
if score >= 90:
print("You got an A.")
break
else:
print("You failed.")
while(1):
score = int(input("Please enter your score pal: "))
if score >= 80:
print("You passed the exam.")
if score >= 90:
print("You got an A in the exam.")
break
else:
print("You failed the exam.")

I don't know why my 'elif ' and 'else' code does not work

score = input("What is your score? ")
if score == str(100):
print('Perfect!')
elif score <= str(range(95, 99)):
print('Great!')
elif score <= str(range(90, 95)):
print('Good')
else:
print('Fail')
It works when I type 95 to 100, but it doesn't work when I type other numbers.
Use ints to compare numbers, not strings:
score = int(input("What is your score? "))
if score == 100:
print('Perfect!')
elif score in range(95, 100): # This 100 catches the 99 case
print('Great!')
elif score in range(90, 95):
print('Good')
else:
print('Fail')

Make a grading Calculator with Functions?

I am doing a school assignment, and can not figure out despite how much I have tried, how to make the raw input change except to make each one individually,
Below is my assignment parameters:
Prompt the user to enter in a numerical score for a Math Class. The numerical score should be between 0 and 100.
Prompt the user to enter in a numerical score for a English Class. The numerical score should be between 0 and 100.
Prompt the user to enter in a numerical score for a PE Class. The numerical score should be between 0 and 100.
Prompt the user to enter in a numerical score for a Science Class. The numerical score should be between 0 and 100.
Prompt the user to enter in a numerical score for an Art Class. The numerical score should be between 0 and 100.
Call the letter grade function 5 times (once for each class).
Output the numerical score and the letter grade for each class.
And this is what I have done:
class_list=(['Math','Science','English','P.E.','Art'])
for i in class_list:
grades = int(input('What is your score for math: ',class_list[1])),
def score (grade):
if grade>=93:
return ("A")
elif grade >=90 and grade<=93:
return ("-A")
elif grade >= 87 and grade<=90:
return("B+")
elif grade>=83 and grade >=87:
return ("B")
elif grade >= 80 and grade >=83:
return ("B-")
elif grade >= 77 and grade >80:
return ("C+")
elif grade >= 73 and grade >= 77:
return ("C")
elif grade >= 70 and grade >=73:
return ("C-")
elif grade>= 67 and grade >= 70:
return ("D+")
elif grade >=63 and grade >=67:
return ("D")
elif grade >=60 and grade >=63:
return ("D-")
else:
return ("F")
print ("For an average score of"),grade, ("your grade is %s") % (score (grade))
Any advice?
For Python 3.2+ (iirc)
# convert the things to Py 2.x :); The below is one of the way;
class_list=['Math','Science','English','P.E.','Art']
my_dict = {}
for (idx,subject) in enumerate(class_list):
my_dict[subject] = int(input(f"What is your score for {class_list[idx]} :")) # my_dict.update({subject : int(input(f"What is your score for {class_list[idx]} : "))})
# Calculate letter grade of each student
def assign_letter_grade(grade):
if grade >= 93:
return ("A")
elif grade >= 90 and grade <= 93:
return ("A-")
elif grade >= 87 and grade <= 90:
return("B+")
elif grade>=83 and grade >= 87:
return ("B")
elif grade >= 80 and grade >= 83:
return ("B-")
elif grade >= 77 and grade > 80:
return ("C+")
elif grade >= 73 and grade >= 77:
return ("C")
elif grade >= 70 and grade >=73:
return ("C-")
elif grade>= 67 and grade >= 70:
return ("D+")
elif grade >= 63 and grade >= 67:
return ("D")
elif grade >= 60 and grade >= 63:
return ("D-")
else:
return ("F")
for subject in my_dict:
print(f"For {subject}, a score of {my_dict[subject]}, you are eligible to get grade as {assign_letter_grade(my_dict[subject])}")
For Python 2.x
class_list=['Math','Science','English','P.E.','Art']
my_dict = {}
for idx,subject in enumerate(class_list):
print("What is your score for :", class_list[idx])
my_dict[subject] = int(raw_input())
print(my_dict)

Python : How to print individual element of Tuple in print function

My code:
def askTestScores():
test1 = int(input("Enter test 1 score: "))
test2 = int(input("Enter test 2 score: "))
test3 = int(input("Enter test 3 score: "))
test4 = int(input("Enter test 4 score: "))
test5 = int(input("Enter test 5 score: "))
testScores = (test1,test2,test3,test4,test5)
return testScores
def determine_grade(testScores):
for i in range(testScores):
if i >= 90 and i <= 100:
return "A"
elif i >= 80 and i <= 89:
return "B"
elif i >= 70 and i <= 79:
return "C"
elif i >= 60 and i <= 69:
return "D"
else:
return "F"
def displayGrades(testScores):
print("Score\t\t Grade")
print("-----------------------")
print(f"{testScores[0]}\t\t {determine_grade(testScores[0])}")
print(f"{testScores[1]}\t\t {determine_grade(testScores[1])}")
print(f"{testScores[2]}\t\t {determine_grade(testScores[2])}")
print(f"{testScores[3]}\t\t {determine_grade(testScores[3])}")
print(f"{testScores[4]}\t\t {determine_grade(testScores[4])}")
x = askTestScore
displayGrades(x)
This is only printing "F" when I use the range function in the loop for determine_grade function. What I want is, to print each grade individually, but when I remove the range function I am getting type error: saying int object not iterable. How can I solve this?
instead of range() Use:
for i in testscores :
Why do you need for in determine_grades ?
I think you meant this
def determine_grades(testScore):
if testScore >= 90 and testScore <= 100:
return "A"
elif testScore >= 80 and testScore <= 89:
return "B"
elif testScore >= 70 and testScore <= 79:
return "C"
elif testScore >= 60 and testScore <= 69:
return "D"
else:
return "F"
def displayGrades(testScores):
print("Score\t\t Grade")
print("-----------------------")
for testScore in testScores:
print(f"{testScore}\t\t {determine_grades(testScore)}")
Here you loop testScores in displayGrades to pass one score at a time to determine_grade and print them.
Because if you used for loop in determine_grades then you need to return a list of grades and not String.
You can improve make your code by appending the inputs to a list instead of tuple
def askTestScores():
testScores = []
for i in range(5):
score = int(input(f"Enter test {i + 1} score: "))
testScores.append(score)
return testScores
EDIT:
If you need tuple you can convert the list to tuple
def askTestScores():
testScores = []
for i in range(5):
score = int(input(f"Enter test {i + 1} score: "))
testScores.append(score)
testScores = tuple(testScores)
return testScores
the easiest way to do it is to include each item of your tuple in a list and remove the range function:
def askTestScores():
test1 = int(input("Enter test 1 score: "))
test2 = int(input("Enter test 2 score: "))
test3 = int(input("Enter test 3 score: "))
test4 = int(input("Enter test 4 score: "))
test5 = int(input("Enter test 5 score: "))
testscores = ([test1],[test2],[test3],[test4],[test5])
return testscores
def determine_grade(testscores):
for i in testscores:
if i >= 90 and i <= 100:
return "A"
elif i >= 80 and i <= 89:
return "B"
elif i >= 70 and i <= 79:
return "C"
elif i >= 60 and i <= 69:
return "D"
else:
return "F"
def displayGrades(testscores):
print("Score\t\t Grade")
print("-----------------------")
print(f"{testscores[0]}\t\t {determine_grade(testscores[0])}")
print(f"{testscores[1]}\t\t {determine_grade(testscores[1])}")
print(f"{testscores[2]}\t\t {determine_grade(testscores[2])}")
print(f"{testscores[3]}\t\t {determine_grade(testscores[3])}")
print(f"{testscores[4]}\t\t {determine_grade(testscores[4])}")
x = askTestScores()
displayGrades(x)
displayGrades(x)

Categories