Issue
I tried to create a function with two "for loops" to compare the elements of two lists and generate an output for each comparison, but my output is always 0.
What is wrong with my code?
UserInput = ["A", "B", "A"]
CorrectAnswers = ["A", "B", "B"]
FalseAnswers = ["B", "A", "A"]
def run():
score = 0
for i in UserInput:
for z in CorrectAnswers:
if i is z:
score = score + 1
elif i is not z:
for p in FalseAnswers:
if p is x:
score = score - 1
else:
raise ValueError("Invalid Input")
print(score)
run()
Thanks a lot in advance...and please don't decry me again.
Solution
Part of the issue with your original code is that you reference "x" but it isn't assigned. I modified your code more than intended but you it looked like you wanted to use the enumerate() function where you can create an index based one one list and use it reference that same position in another list.
def run():
score = 0
for i, j in enumerate(UserInput):
if j == CorrectAnswers[i]:
score = score+1
elif j == FalseAnswers[i]:
score = score-1
else:
raise ValueError("Invalid Input")
return(score)
print(run())
Answered By - Omnishroom
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.