list = []
jim = 70
list.append('Jim')
parry = 36
list.append('parry')
ada = 68
list.append('ada')
jevon = 25
list.append('jevon')
samuel = 94
list.append('samuel')
bryant = 81
list.append('bryant')
sia = 12
list.append('sia')
print(list)
for s in list:
if s>90:
print(f"Congratulations! \n{s} got the gold medal award.")
elif s>80:
print(f"Congratulations! \n{s} got the silver medal award.")
elif s>70:
print(f"Congratulations! \n{s} got the bronze medal award.")
elif s>=60:
print(f"Congratulations! \n{s} has passed the exam.")
elif s<60:
print(f"{s} I am sorry to inform that you have failed the exam.")
CodePudding user response:
Your list contains the strings ['Jim', 'parry', 'ada', ...]
. These strings have no relations to the variables that share the same names. So you end up checking for conditions like 'Jim' > 90
, which makes no sense. You probably want to use a dict
here. For example:
grades = {
'Jim': 70,
'Parry': 36,
'Ada': 68,
'Jevon': 25,
'Samuel': 94,
'Bryant': 81,
'Sia': 12,
}
print(grades)
for name, grade in grades.items():
if grade > 90:
print(f"Congratulations! \n{name} got the gold medal award.")
elif grade > 80:
print(f"Congratulations! \n{name} got the silver medal award.")
elif grade > 70:
print(f"Congratulations! \n{name} got the bronze medal award.")
elif grade > =60:
print(f"Congratulations! \n{name} has passed the exam.")
elif grade < 60:
print(f"{name} I am sorry to inform that you have failed the exam.")
Also just a note: You shouldn't name a variable list
, because it shadows the built-in list
type. Same for all of the built-in functions.
CodePudding user response:
P.S. You probably want the first element to be jim
instead of Jim
You will also want to change the name of list
because it will override(shadow) the python stantard type list
In your case, you probably want the following:
for i in lst:
s = eval(i)
if s>90:
print(f"Congratulations! \n{i} got the gold medal award.")
elif s>80:
print(f"Congratulations! \n{i} got the silver medal award.")
elif s>70:
print(f"Congratulations! \n{i} got the bronze medal award.")
elif s>=60:
print(f"Congratulations! \n{i} has passed the exam.")
elif s<60:
print(f"{i} I am sorry to inform that you have failed the exam.")
It will look for the proper variables in current scope.
Output:
['jim', 'parry', 'ada', 'jevon', 'samuel', 'bryant', 'sia']
Congratulations!
jim has passed the exam.
parry I am sorry to inform that you have failed the exam.
Congratulations!
ada has passed the exam.
jevon I am sorry to inform that you have failed the exam.
Congratulations!
samuel got the gold medal award.
Congratulations!
bryant got the silver medal award.
sia I am sorry to inform that you have failed the exam.
The exception is because(not like comparing char
and int
in C/C , which compares ASCII codes) comparing strings to integers make no sense.