Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If statement ignored
#1
I am making a program to test the user on 10 math questions in a random order. At the end they can try again or end it and get their score. For some reason the if statement to check if the user's answers is correct is not working and neither is the else statement so it is being ignored. Any help is appreciated.
import random

test = 0
points = 0

mathsQs = {1:"17 * 3", 2:"3² * √36", 3:"(20% * 50)(10% * 100)", 4:"(5*5)* 40%", 5:"Is 30% of 150 bigger smaller or equal to 15% of 300?", 6:"17 * 6", 7:"5² * √400", 8:"(4/5)(15/28)", 9:"12% of 32", 10:"102/(34*1.5)"}
answers = ["b", "a", "d", "d", "c", "a", "c", "b", "d", "d"]

m1 = {"a":54, "b":51, "c":21, "d":49}
m2 = {"a":54, "b":51, "c":21, "d":49}
m3 = {"a":87, "b":110, "c":91, "d":100}
m4 = {"a":32, "b":10, "c":100, "d":8}
m5 = {"a":"Smaller", "b":"Equal to", "c":"Equal to"}
m6 = {"a":102, "b":98, "c":100, "d":149}
m7 = {"a":245, "b":435, "c":500, "d":600}
m8 = {"a":5/7, "b":3/7, "c":2/9, "d":1/3}
m9 = {"a":4, "b":5.55, "c":9.17, "d":3.84}
m10 = {"a":1.73, "b":1.9649122807, "c":1.98039215686, "d":2}


while test == 0:
  print("")
  print("Welcome to the maths quiz")
  name = input("What is your name? ")
  nums = [1,2,3,4,5,6,7,8,9,10]
  random.shuffle(nums)
  for i in range(0, 9):
    x = mathsQs.get(nums[i])
    print(x)
    y = "{}{}".format("m", nums[i])
    print("Choose a the correct answer")
    for key, value in (locals()[y]).items():
      print("{}: {}".format(key, value))
    user_ans = input("")
    ans = answers[nums[i]]
    if user_ans == ans:
      points += 1
      "Correct!"
    else:
      "Incorrect"
  test = int(input("Type 0 to play again or any other numbers to stop: "))
  if test != 0:
    print("{} got {} points".format(name, points))
    test = 1
Thanks!
Reply
#2
How are you determining that the if/else is being ignored? Exactly one of those branches is being executed. On lines 38 and 40, you're doing nothing with those strings - did you mean to print them?
Reply
#3
Line 38 and 40 - you forgot print()
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020