-5

I just finished my code, and there's kind of a problem to it: In fact, one of the variable's value isn't changing even tho I add 1 to it everytime the user input the correct answer, which I simulated, and noticed that at the end, when I call my variable, it is showing 0. The code is about asking the user to answer 20 multiplications within 40 seconds, and at the end, it will tell the user how many questions did he skipped, and how many questions did he answer good.

import time

seconds = 40


def Timer(seconds, i):
    print("Start timer : 40s")
    for i in range(seconds):
        seconds -= 1
    if seconds == 0:
        print("End")
    time.sleep(1)


errorNumbers = 0
goodAnswers = 0

multi1 = "7*7"
multi2 = "3*6"
multi3 = "4*2"
multi4 = "8*2"
multi5 = "1*2"
multi6 = "9*6"
multi7 = "5*8"
multi8 = "6*4"
multi9 = "5*9"
multi10 = "3*4"
multi11 = "8*2"
multi12 = "6*5"
multi13 = "2*9"
multi14 = "1*1"
multi15 = "8*8"
multi16 = "5*9"
multi17 = "9*10"
multi18 = "4*7"
multi19 = "8*5"
multi20 = "6*6"
phraseRep = "what's the answer of"
rep1 = input(phraseRep + " " + multi1)
if rep1 == 49:
    rep2 = input(phraseRep + " " + multi2)
    goodAnswers += 1
elif rep1 == "":
    errorNumbers += 1
    rep2 = input(phraseRep + " " + multi2)
elif rep1 != 49:
    rep2 = input(phraseRep + " " + multi2)

if rep2 == 18:
    rep3 = input(phraseRep + " " + multi3)
    goodAnswers += 1
elif rep2 == "":
    rep3 = input(phraseRep + " " + multi3)
    errorNumbers += 1
elif rep2 != 18:
    rep3 = input(phraseRep + " " + multi3)

if rep3 == 8:
    rep4 = input(phraseRep + " " + multi4)
    goodAnswers += 1
elif rep3 == "":
    rep4 = input(phraseRep + " " + multi4)
    errorNumbers += 1
elif rep3 != 8:
    rep4 = input(phraseRep + " " + multi4)

if rep4 == 16:
    rep5 = input(phraseRep + " " + multi5)
    goodAnswers += 1
elif rep4 == "":
    rep5 = input(phraseRep + " " + multi5)
    errorNumbers += 1
elif rep4 != 16:
    rep5 = input(phraseRep + " " + multi5)

if rep5 == 2:
    rep6 = input(phraseRep + " " + multi6)
    goodAnswers += 1
elif rep5 == "":
    rep6 = input(phraseRep + " " + multi6)
    errorNumbers += 1
elif rep5 != 2:
    rep6 = input(phraseRep + " " + multi6)

if rep6 == 54:
    rep7 = input(phraseRep + " " + multi7)
    goodAnswers += 1
elif rep6 == "" :
    rep7 = input(phraseRep + " " + multi7)
    errorNumbers += 1
elif rep6 != 54:
    rep7 = input(phraseRep + " " + multi7)

if rep7 == 40:
    rep8 = input(phraseRep + " " + multi8)
    goodAnswers += 1
elif rep7 == "":
    rep8 = input(phraseRep + " " + multi8)
    errorNumbers += 1
elif rep7 != 40:
    rep8 = input(phraseRep + " " + multi8)


if rep8 == 24:
    rep9 = input(phraseRep + " " + multi9)
    goodAnswers += 1
elif rep8 == "":
    rep9 = input(phraseRep + " " + multi9)
    errorNumbers += 1
elif rep8 != 24:
    rep9 = input(phraseRep + " " + multi9)

if rep9 == 45:
    rep10 = input(phraseRep + " " + multi10)
    goodAnswers += 1
elif rep9 == "":
    rep10 = input(phraseRep + " " + multi10)
    errorNumbers += 1
elif rep9 != 45:
    rep10 = input(phraseRep + " " + multi10)

if rep10 == 12:
    rep11 = input(phraseRep + " " + multi11)
    goodAnswers += 1
elif rep10 == "":
    rep11 = input(phraseRep + " " + multi11)
    errorNumbers += 1
elif rep10 != 12:
    rep11 = input(phraseRep + " " + multi11)

if rep11 == 16:
    rep12 = input(phraseRep + " " + multi12)
    goodAnswers += 1
elif rep11 == "":
    rep12 = input(phraseRep + " " + multi12)
    errorNumbers += 1
elif rep11 != 16:
    rep12 = input(phraseRep + " " + multi12)

if rep12 == 30:
    rep13 = input(phraseRep + " " + multi13)
    goodAnswers += 1
elif rep12 == "":
    rep13 = input(phraseRep + " " + multi13)
    errorNumbers += 1
elif rep12 != 30:
    rep13 = input(phraseRep + " " + multi13)

if rep13 == 18:
    rep14 = input(phraseRep + " " + multi14)
    goodAnswers += 1
elif rep13 == "":
    rep14 = input(phraseRep + " " + multi14)
    errorNumbers += 1
elif rep13 != 18:
    rep14 = input(phraseRep + " " + multi14)

if rep14 == 1:
    rep15 = input(phraseRep + " " + multi15)
    goodAnswers += 1
elif rep14 == "":
    rep15 = input(phraseRep + " " + multi15)
    errorNumbers += 1
elif rep14 != 1:
    rep15 = input(phraseRep + " " + multi15)

if rep15 == 64:
    rep16 = input(phraseRep + " " + multi16)
    goodAnswers += 1
elif rep15 == "":
    rep16 = input(phraseRep + " " + multi16)
    errorNumbers += 1
elif rep15 != 64:
    rep16 = input(phraseRep + " " + multi16)


if rep16 == 45:
    rep17 = input(phraseRep + " " + multi17)
    goodAnswers += 1
elif rep16 == "":
    rep17 = input(phraseRep + " " + multi17)
    errorNumbers += 1
elif rep16 != 45:
    rep17 = input(phraseRep + " " + multi17)

if rep17 == 90:
    rep18 = input(phraseRep + " " + multi18)
    goodAnswers += 1
elif rep17 == "":
    rep18 = input(phraseRep + " " + multi18)
    errorNumbers += 1
elif rep17 != 90:
    rep18 = input(phraseRep + " " + multi18)


if rep18 == 28:
    rep19 = input(phraseRep + " " + multi19)
    goodAnswers += 1
elif rep18 == "":
    rep19 = input(phraseRep + " " + multi19)
    errorNumbers += 1
elif rep18 != 28:
    rep19 = input(phraseRep + " " + multi19)


if rep19 == 40:
    rep20 = input(phraseRep + " " + multi20)
    goodAnswers += 1
elif rep19 == "":
    rep20 = input(phraseRep + " " + multi20)
    errorNumbers += 1
elif rep19 != 40:
    rep20 = input(phraseRep + " " + multi20)

if rep20 == 36:
    goodAnswers += 1
elif rep20 == "":
    errorNumbers += 1
elif rep20 != 36:
    print("end")

print(goodAnswers)
print("You didn't answer to " + str(errorNumbers) + " questions.")
print("You have" + str(goodAnswers) + " good answers.")

marw4ne
  • 1
  • 3
  • 1
    What version of Python is this? – Mureinik Mar 13 '20 at 22:16
  • it's python 3.8.2 – marw4ne Mar 13 '20 at 22:17
  • 4
    @marw4ne `input` returns a string in Python 3. All of your comparisons of the form `rep1 == 49` will evaluate to `False`. For future question, please only provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your problem rather than pasting entire projects. – Brian61354270 Mar 13 '20 at 22:18
  • So, how can I do to make it that it returns an int ? – marw4ne Mar 13 '20 at 22:21
  • Sorry about this, will do :) – marw4ne Mar 13 '20 at 22:22
  • Your timer doesn't do much. You probably want the `time.sleep` *in* the loop, not after it. The `if` statement isn't necessary; if the loop iterates as many times as you request, `seconds` *will* be 0. – chepner Mar 13 '20 at 22:26
  • Thank you so much for your answers. However, I'm still wondering about how to do to make the multiplications etc (the biggest part of the code) shows while the timer is working ? – marw4ne Mar 13 '20 at 23:25
  • Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – AMC Mar 14 '20 at 00:18
  • Variable and function names should follow the `lower_case_with_underscores` style. – AMC Mar 14 '20 at 00:18

1 Answers1

2

input() in Python 3 returns a string, and you're comparing the answers to numbers. Compare them to strings instead, and you should be OK. E.g:

if rep1 == '49':
    # Here-^--^
    rep2 = input(phraseRep + " " + multi2)
    goodAnswers += 1
Mureinik
  • 297,002
  • 52
  • 306
  • 350