-2

I'm very new in Coding and I started with Python to understand how to code. I'm going to program a simple quiz, where the User choose which Quiz they want to play. I did it the input() and yet it does not go to the if - Statement. Code:

import quiz1
import quiz2
import quiz3
import quiz4

quiz = input("Please choose which Quiz you want to play (1-4): ")
print(quiz)

if quiz == 1:
        quiz1
        #f1 = open(fragebogen1.py)
        #print(f1)
elif quiz == 2:
        print("hello")
elif quiz == 3:
        print("hello")
elif quiz == 4:
        print("hello")

Thanks in advance

Sakhri Houssem
  • 975
  • 2
  • 16
  • 32
Kaan
  • 1
  • 3
  • 3
    `input()` returns a string. You are comparing that string against an integer and that check can never be `True`. check for `if quiz == '1'`. – Ma0 Feb 16 '18 at 10:53

3 Answers3

3

The input function returns a string. You need to convert the user's input to integer:

quiz = int(input("Please choose which Quiz you want to play (1-4): "))
sjw
  • 6,213
  • 2
  • 24
  • 39
3

Convert input to integer before checking

if int(quiz) == 1:
kawadhiya21
  • 2,458
  • 21
  • 34
  • checking against a string is better. `int(quiz)` can raise an Error, `if quiz == '1':` cannot + you do not actually need an integer here; no math is done. – Ma0 Feb 16 '18 at 10:54
  • May I know why ? – kawadhiya21 Feb 16 '18 at 10:55
  • 2
    If you are going to wrap the input, you should probably do it as the input is received (i.e. `quiz = int(input("..."))`) otherwise you'll need to do it for each `elif` – asongtoruin Feb 16 '18 at 10:55
  • i tested both, but the didnt solve the problem i want to say python " Hey open the other file "quiz1" and read it" After i wrote a number from 1 - 4 it just write it down and stops – Kaan Feb 16 '18 at 11:00
-1

The elif == 1 cannot be reached, since you put in an if quiz == 1 above. Instead you need to put in elif quiz == 2:.