0
why this program is giving me error
total_students=6 failed_students=["Ali","ahmed","Abdulmateen"] failed_students [0]=9 failed_students [1]=5 failed_students [2]=2 if failed_students([0]<[1] and [2]): print(failed_students[0],"has the least numbers") elif failed_students[1]<[0] and [2]: print(failed_students[1],"has the least numbers") elif failed_students[2]<[0] and [1]: print(failed_students[2],"has the least numbers") print("Better luck next time")
5 Antworten
+ 3
[0]<[1] implies nothing ,it should be student[0]<student[1] and student[2]
+ 7
Your code may run after some improofments, but let me give you some friendly hints to do this job a bit more pythonic. It's not as difficult as it looks like, but it needs some more basic knowledge from python regarding dictionary.
(1) total_students is a var that is created but never used
(2) a list failed_students is created and initialzesd with 3 names
(3) in the next line these names will be replaced by the score
(4) then it compares the values against each others
From my point of view, a dictionary would be a better data structure to do this
# show student with the least number
failed_students = {"Ali":9, "Ahmed":5, "Abdulmateen":2} # create the dict
least = max(failed_students.values()) # initialize var for least score
for name, score in failed_students.items(): # use a for loop to iterate through the dict
if score < least:
tmp_name, least = name, score
print(tmp_name, least)
+ 3
You Have To Configure And Use Syntax Properly In Which Variable You Want To Perform Indexing.
In Above Program You Made Mistake During Performing Boolean Expression.
This Will Be Like-
if failed_students[0]<failed_students[1] and failed_students[0]<failed_students[2]:
+ 2
I am a beginner i hope i will learn this structure in future
Thanks for your answer
+ 1
Thanks