[Problem sentence]
Create a program to display the following results when you give a certain exam number num.Change the num value (for example, "A01", "A02", "A07") and check the program operation.
We are making products that meet the above three conditions.
I tried it myself until the end, but I'm stuck.
result={"A01":50, "A03":80, "A04":100, "A05":20, "A07":60}
num = 'A03'
if num in result:
print(" Passed")
elif 0<=num<60:
print ("Failed")
else:
print("Not Received")
Until A01, a conditional evaluation is displayed, but after that, the error TypeError: '<='not supported between instances of 'int' and 'str'
.
Please let me know where the mistake is and what kind of correction I should make.
Note: I edited it to give you more information!
I'm sorry that you told me...Thank you for your cooperation!
Perhaps you are confused with the examination number and score.
For the variable num
, num
contains the examination number string 'A03'
, as you substitute num='A03'
.The score is not included.The error you see this time is <=
, so if you think it's a number, you can't do it because it's a string. In fact, num
is a string, so you can't compare 0<=num
.
You have to get it from the dictionary result
to know the score.For example, 'A03'
scores can be obtained by writing result['A03']
.
By the way, with the current method of conditional branching, it doesn't work well even if you correct the confusion between the examination number and the score.At this rate, result
just stores the score, and it will print "pass".Check carefully how the if statement works.
Based on the three conditions, is this what you wanted to do?
students=["A01", "A02", "A03", "A04", "A05", "A06", "A07" ]
result = {"A01":50, "A03":80, "A04":100, "A05":20, "A07":60}
for student in students:
if not student in result.keys():
print("Not Received")
elif result [student] >=60:
print(" Passed")
else:
print ("Failed")
Regarding the program presented in the questionnaire, first of all,
if num in result:
I think the loop and conditional statements are mixed up.In order to create a loop statement, the basics of a loop are to prepare a list of IDs of all examinees as students
and use them one by one.That's
students=["A01", "A02", "A03", "A04", "A05", "A06", "A07" ]
for student in students:
(hereinafter referred to as Loop Block)
That's the part.
Next, since it is a dictionary, you need to take the value from the key and use it.That's the part that says result[student]
and only then can you compare it to the number 60.
elif result [student]>=60:
Finally, we also have to deal with cases where we haven't received them, so we use conditional branches to determine which IDs are not included in the result key list.
if not student in result.keys():
© 2024 OneMinuteCode. All rights reserved.