Why does the first item in the list come out as incorrect ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why does the first item in the list come out as incorrect ?

When given the question with the first item (1<5) in the list. Inputing the answer 5 which is stored in another list gives me incorrect, the rest of the items in list work perfect. I’m still in intermediate level in python. Does somehow know what’s wrong with my code? https://code.sololearn.com/ci4diCz290kU/?ref=app

7th May 2022, 6:45 AM
ozzy
ozzy - avatar
3 Answers
+ 1
Rearrange your list, inside each quotation Marks seperate them with commas The greater than sign is a comparism operator and will only give you a Boolean value import random question_string = ["5, 1", "2, 1", "3, 1"] question_answers = [5 , 2 , 3] a = random.choice(question_string) print(a) for i in a: a == question_answers user_input = input("Which number is bigger?: ") for x in a: if x == user_input: print("Correct") break else: print("Incorrect") break Also try to write the larger values before the smaller value
9th May 2022, 2:44 AM
cheta
cheta - avatar
0
Hi You need to convert your input into an integrer for comparison and also you need to check for the specific location. I am a beginner myself so excuse me for messing up your code, but this seems to work: import random question_string = ["1<5", "2>1", "3>1"] question_answers = [5, 2, 3] a = random.choice(question_string) print(a) y= question_string.index(a) for i in a: a == question_answers user_input = input("Which number is bigger?: ") print(user_input) stuff= question_answers[y] print (stuff) if int(stuff) == int(user_input): print("correct") else: print("incorrect") print(user_input) print(stuff) for x in a: if x == stuff: print("Correct") break else: print("Incorrect") break
7th May 2022, 7:32 AM
Dr. Steffen Lesle
0
You can try this code I wrote, I made it a loop with continue and break statement so the program doesn't end when user input wrong answer I'm also a beginner and I hope this helps import random while True: numbers = ["4, 6", "7, 8", "10, 2", "4 ,20"] answers = ["6", "8", "10", "20"] incorrect_answers = ["4", "7", "2"] a = random.choice(numbers) print(a) userinput = input("Enter the bigger number: ") if userinput in answers: print(userinput, "Is the correct answer!") break elif userinput in incorrect_answers: print(userinput, "is not the correct answer, try again") continue else: print("Invalid input, try again!")
9th May 2022, 3:30 AM
cheta
cheta - avatar