Skip to content
Advertisement

How to limit incorrect answers from users input?

I’m here with my code, here you can see it:

def generate_integer(level):
    score = 0
    i = 0
    false = 0
    level = int(level)
    while i != 10:

            # Choosing the numbers of digit if 1 >> 1-9 / if 2 >> 11-99 / if 3 >> 100-999
            end = 10**level-1

            # Define x and y
            x = random.randint(0,end)
            y = random.randint(0,end)
            answer = x + y

            # Users cal
            user = int(input(f'{x} + {y} = '))
            if user == answer:
                score = score + 1
            while user != answer:
                false + 1
                print('EEE')
                user = int(input(f'{x} + {y} = '))
            if false == 3:
                print(f'{x} + {y} = {answer}')


            i = i + 1

    print(f'score: {score}/10')

Let me explain: I defined false for, if user inputs the answer 3 times and all of them for that question are false, show user the answer and continue asking

Actually this code asks 10 different math questions, this is a part of my code, I’m checking if answer is not true print(‘EEE’) and re ask it again, but if user tries 3 time and all incorrect, then I show the answer, pass that question and keep asking other questions.

If you have any ideas for re asking question, when users input was non-numerical, I’ll be thankful.

Advertisement

Answer

def check(user_answer,correct_answer):
    for a chance in range(2):
        print("Wrong answer, try again") 
        user_answer=input('User please type your answer for the question')
        if user_answer == correct_answer:
            return 'True' # Given the Right answer
        else:
            print('Again wrong answer')
    return 'False' #Given all wrongs answers

user_answer=input('User please type your answer for the question')

correct_answer=10

if user_answer != correct_answer:
    result=check(user_answer,correct_answer)

    if result:
        print("your answer is correct")
    else:
        print("your all answers were wrong, the right answer is: ",correct_answer)

else:
    print("Perfect your answer was right in the first guess")
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement