-4

I'm trying to validate some user input. I am creating a multi-answer quiz, which allows the user to answer a,b,c or d. I am trying to validate the user input, so if they were to enter nothing or and other letters it would pop allow them to answer the same question again. However when I run my code, if the user was to insert "g" for example, it would show "you are wrong" in the terminal and move onto the next question.

def quiz_game():
    questions_num = 0
    score = 0

    for key in questions:
        print("...................")
        print(key)
        for x in answers[questions_num]:
            print(x)        
        guess = input('Enter a, b, c or d: ')  
        validate_input(input)       
        if answer_input(guess, questions_num):                  
            print('Your answer is correct')
            questions_num += 1
            score += 1 
        else:                    
            print('Your answer is wrong')
            questions_num += 1

    points(score)

def validate_input(guess):
    guess = ('Enter a, b, c or d: ')
    while not guess:
        print('Enter a, b, c or d: ')
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Damhán
  • 1
  • 2
  • 4
    "*it would show "you are wrong" in the terminal*" The code you've posted here is not capable of printing such a string; can you please ensure that what you've described and the code you've provided here is representative of what you're *actually* working with and conforms to our guidance on creating a [mre], as well as [ask]? – esqew Nov 10 '21 at 12:27
  • could you show what answer_input is ? – Tirterra Nov 10 '21 at 12:28
  • 2
    Your `validate_input` doesn't do what you think it does, in fact it really.. doesn't do anything. – Zaid Al Shattle Nov 10 '21 at 12:32
  • The problem is your validate input which is a mess and is not applied to guess but input – JuR Nov 10 '21 at 12:36

1 Answers1

0

Try with these modifications it should work assuming your other functions are okay

def quiz_game():
    questions_num = 0
    score = 0

    for key in questions:
        print("...................")
        print(key)
        for x in answers[questions_num]:
            print(x)        
        guess = input('Enter a, b, c or d: ')  
        validate_input(guess)       
        if answer_input(guess, questions_num):                  
            print('Your answer is correct')
            questions_num += 1
            score += 1 
        else:                    
            print('Your answer is wrong')
            questions_num += 1

    points(score)

def validate_input(guess):
    suitable_answer = ['a', 'b', 'c', 'd']
    while not guess in suitable_answer:
        guess = input('Enter a, b, c or d: ')
JuR
  • 113
  • 4
  • Thank you JuR, this helped greatly. I am new to python so just learning the ropes. Thank you again for you help – Damhán Nov 10 '21 at 13:10