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)
Related
I am new to python and trying to make a grade calculator with functions. I want to check the users input so that when they put a number in it will check and see if it is between 1 and 100, but don't know how to do that. can anyone help? I also want to make it at the end so that it says for 'subject' and a score of 'number' you get a 'grade'.
here is what i have so far:
class_list = ['math', 'art', 'P.E.', 'science', 'english']
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
def assign_letter_grade(grade):
if 93 <= grade <= 100:
return 'A'
elif 90 <= grade < 93:
return 'A-'
elif 87 <= grade < 90:
return 'B+'
elif 83 <= grade < 87:
return 'B'
elif 80 <= grade < 83:
return 'B-'
elif 77 <= grade < 80:
return 'C+'
elif 73 <= grade < 77:
return 'C'
elif 70 <= grade < 73:
return 'C-'
elif 67 <= grade < 70:
return 'D+'
elif 63 <= grade < 67:
return 'D'
elif 60<= grade < 63:
return 'D-'
else:
return 'F'
Try it:
def raw_input():
while True:
score = input()
if score in [str(i) for i in range(101)]:
return score
class_list = ['math', 'art', 'P.E.', 'science', 'english']
my_dict = {}
for idx,subject in enumerate(class_list):
print ("What is your score for ", class_list[idx])
my_dict[subject] = assign_letter_grade(int(raw_input()))
print( my_dict)
def assign_letter_grade(grade):
if 93 <= grade <= 100:
return 'A'
elif 90 <= grade < 93:
return 'A-'
elif 87 <= grade < 90:
return 'B+'
elif 83 <= grade < 87:
return 'B'
elif 80 <= grade < 83:
return 'B-'
elif 77 <= grade < 80:
return 'C+'
elif 73 <= grade < 77:
return 'C'
elif 70 <= grade < 73:
return 'C-'
elif 67 <= grade < 70:
return 'D+'
elif 63 <= grade < 67:
return 'D'
elif 60<= grade < 63:
return 'D-'
else:
return 'F'
You can use the try and except functions.
For example:
data_valid = False
score = input("What is your score?")
while data_valid == False:
score = input("Enter your score: ")
try:
score = float(score)
except:
print("Invalid input. Please type an decimal or integer.")
continue
if score < 0 or score > 100:
print("Invalid input. Score should be between 0 and 100.")
continue
else:
data_valid = True
This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 6 months ago.
Brand new to python. When using the map function in python, i get a series of the word "none" as an output when i run my function. ive added screenshot below.
Code
def rawGradeToLetter(grade):
if grade >= 90:
print ("A")
elif grade < 90 and grade >= 80:
print ("B")
elif grade < 80 and grade >= 70:
print ("C")
elif grade < 70 and grade >= 60:
print ("D")
elif grade < 60:
print ("F")
def convertRawsToLetters(myList):
return list(map(rawGradeToLetter, myList))
Example run:
>>> convertRawsToLetters([90,80,70])
A
B
C
[None, None, None]
I have a feeling it has something to do with the word list before the map function, but if I remove that all I get is the address of the map like "map object at 0x8g9b7ea51950".
You need to return the grades, not print them. Printing the grades displays them on screen instead of giving a value back to the caller.
def rawGradeToLetter(grade):
if grade >= 90:
return "A"
elif grade < 90 and grade >= 80:
return "B"
elif grade < 80 and grade >= 70:
return "C"
elif grade < 70 and grade >= 60:
return "D"
elif grade < 60:
return "F"
Without a return statement your function implicitly returns None, which explains why you're seeing [None, None, None] in your output.
I think it is because you aren't returning anything on your map function, so it doesn't work correctly. You can try this, so the instead of being mapped to None, it will be mapped to the results:
def rawGradeToLetter(grade):
if grade >= 90:
print ("A")
return "A"
elif grade < 90 and grade >= 80:
print ("B")
return "B"
elif grade < 80 and grade >= 70:
print ("C")
return "C"
elif grade < 70 and grade >= 60:
print ("D")
return "D"
elif grade < 60:
print ("F")
return "F"
def convertRawsToLetters(myList):
return list(map(rawGradeToLetter, myList))
It is, if you want to maintain the prints, if not, you can remove them.
def rawGradeToLetter(grade):
if grade >= 90:
print ("A")
elif grade < 90 and grade >= 80:
print ("B")
elif grade < 80 and grade >= 70:
print ("C")
elif grade < 70 and grade >= 60:
print ("D")
elif grade < 60:
print ("F")
def convertRawsToLetters(myList):
for grade in myList:
rawGradeToLetter(grade)
I think you are over complicating it by using map() if all you want to do is print the grades.
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.")
I'm fairly new to programming and have only been doing so for a month. Currently I'm trying to get user input, store it in a list, and pass that list into a function. I'm having trouble using the list as an argument for my function (the last line of code). Thank in advance!
grade_list = []
percentages = 0
while True:
percentages = input("Enter some numbers here: ")
if percentages == "done":
break
grade_list.append(percentages)
print(grade_list)
def gpaCalc(marks):
gpaList = []
for grade in marks: #sorts data
if grade <= 49.99:
grade = 0.00
elif 50 <= grade <= 52.99:
grade = 0.70
elif 53 <= grade <= 56.99:
grade = 1.00
elif 57 <= grade <= 59.99:
grade = 1.30
elif 60 <= grade <= 62.99:
grade = 1.70
elif 63 <= grade <= 66.99:
grade = 2.00
elif 67 <= grade <= 69.99:
grade = 2.30
elif 70 <= grade <= 72.99:
grade = 2.70
elif 73 <= grade <= 76.99:
grade = 3.00
elif 77 <= grade <= 79.99:
grade = 3.30
elif 80 <= grade <= 84.99:
grade = 3.70
elif 85 <= grade <= 89.99:
grade = 3.90
elif 90 <= grade <= 100:
grade = 4.00
gpaList.append(grade) #gathers data into list
gpaList.sort()
return gpaList
print (gpaCalc(PROBLEM))
You can pass a list as you would pass it normally into any function, just always make sure you are accessing the items in the list by indexing correctly, rather than calculating the whole list. Use the following instead:
def gpaCalc(marks):
gpaList = []
for grade in marks[0]: #sorts data
if grade <= 49.99:
grade = 0.00
elif 50 <= grade <= 52.99:
grade = 0.70
elif 53 <= grade <= 56.99:
grade = 1.00
elif 57 <= grade <= 59.99:
grade = 1.30
elif 60 <= grade <= 62.99:
grade = 1.70
elif 63 <= grade <= 66.99:
grade = 2.00
elif 67 <= grade <= 69.99:
grade = 2.30
elif 70 <= grade <= 72.99:
grade = 2.70
elif 73 <= grade <= 76.99:
grade = 3.00
elif 77 <= grade <= 79.99:
grade = 3.30
elif 80 <= grade <= 84.99:
grade = 3.70
elif 85 <= grade <= 89.99:
grade = 3.90
elif 90 <= grade <= 100:
grade = 4.00
gpaList.append(grade) #gathers data into list
gpaList.sort()
return gpaList
grade_list = []
percentages = 0
while True:
percentages = input("Enter some numbers here: ")
if percentages == "done":
break
grade_list.append(percentages)
print(gpaCalc(grade_list))
keep your check for "done" as is. if it is not done, then convert float.
while True:
percentages = input("Enter some numbers here and 'done' to exit:")
if percentages == "done":
break
try:
grade_list.append(float(percentages))
except ValueError:
pass
sorting...
for grade in marks: #sorts data
.....
gpaList.append(grade) #gathers data into list
#also, sort outside the loop, when done, not each time.
gpaList.sort()
return gpaList
Right before the last print line, define your list of marks, e.g. marks = [70, 68, 50, 89, ...] and pass it to gpaCalc in your function call:
print(gpaCalc(marks))
Note that Python convention says you should not use camel case in your identifiers; use underlines instead: gpa_calc
Edit: I missed the point of the question! To get the user's inputs, use a loop:
def get_user_input():
grades = []
while True:
# take input
value = ... # figure it out
if value == 'q':
break
try:
# do basic validation here
grades.append(int(value))
# might be a good idea to check the range too…
except ValueError:
print("This is not a valid grade!")
return grades
If you would like an explanation, leave a comment!
I am trying to solve this grade input program. I need to input the grades between 0.6 to 1.0 and assign them a letter value. I can only use the if else method as we haven't gotten to the other methods yet in class...
score = raw_input("Enter Score Grade:")
sco = int(float(score))
if score < 0.60:
print "Your grade is an F"
elif score >= 0.6:
print "grade d"
elif score >= 0.7:
print "grade c"
elif score >= 0.8:
print "grade b"
elif score >= 0.9:
print "grade a"
else:
print "wrong score"`
You don't have to go highest to lowest score, you can also do this:
score = float(raw_input("Enter Score Grade:"))
if score < 0.60:
print "Your grade is an F"
elif score < 0.7:
print "grade d"
elif score < 0.8:
print "grade c"
elif score < 0.9:
print "grade b"
elif score <= 1.0:
print "grade a"
else:
print "wrong score"
If you do decide to check from highest to lowest, being consistent is a good practice. You can check your failing grade last:
score = float(raw_input("Enter Score Grade:"))
if score > 1:
print "wrong score"
elif score >= 0.9:
print "grade a"
elif score >= 0.8:
print "grade b"
elif score >= 0.7:
print "grade c"
elif score >= 0.6:
print "grade d"
else:
print "Your grade is an F"
As a reusable function :
def grade(score):
if score > 1:
return "wrong score"
if score >= 0.9:
return "grade a"
if score >= 0.8:
return "grade b"
if score >= 0.7:
return "grade c"
if score >= 0.6:
return "grade d"
return "Your grade is an F"
score = float(raw_input("Enter Score Grade:"))
print grade(score)
You should start from the largest grade first:
as you see 0.92 > 0.6 and also 0.92 > 0.9
But according to yout logic, it will satisfy the first if and will never reach last if.
Do something like this:
score = raw_input("Enter Score Grade:")
sco = int(float(score))
if score < 0.60:
print ("Your grade is an F")
elif score >= 0.9:
print ("grade a")
elif score >= 0.8:
print ("grade b")
elif score >= 0.7:
print ("grade c")
elif score >= 0.6:
print ("grade d")
else:
print ("wrong score")
You need to go from high grade to low grade. You need to change sco = int(float(score)) to score = float(score). It is not required to change it to int as you are comparing float
score = raw_input('Enter the Score')
score = float(score)
if score >= 0.9:
grade = 'A'
elif score >= 0.8:
grade = 'B'
elif score >= 0.7:
grade = 'C'
elif score >= 0.6:
grade = 'D'
else:
grade = 'F'
print 'Your Grade : ' + grade
You have to take not of the first condition. If the first condition is equal to 0.9, it will equate to false and give the grade of 'B'. Also, it will be wise to convert the input to int first
score = int(raw_input('Enter the Score'))
score = float(score)
if score > 0.9:
grade = 'A'
elif score >= 0.8:
grade = 'B'
elif score >= 0.7:
grade = 'C'
elif score >= 0.6:
grade = 'D'
else:
grade = 'F'
print 'Your Grade : ' + grade
It is therefore advisable to use the greater than or equal to so as to handle the situation when it is equal to the upper value(ie 0.9).
score = int(raw_input('Enter the Score'))
score = float(score)
if score >= 0.9:
grade = 'A'
elif score >= 0.8:
grade = 'B'
elif score >= 0.7:
grade = 'C'
elif score >= 0.6:
grade = 'D'
else:
grade = 'F'
print 'Your Grade : ' + grade