Home > Mobile >  how to limit incorrect answers from users input?
how to limit incorrect answers from users input?

Time:08-11

im 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')

well, 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, im checking if answer is not true print('EEE') and re ask it again, but if user tries 3 time and all incorrect, than i show him the answer, pass that question and keep asking other questions

if you have any ideas for re asking question, when users input was non-nummerical, ill be thankfull

CodePudding user response:

You just have an indentation wrong

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}')
                    break


            i = i   1

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

Would probably work, because you want to be checking for how many times they messed up within the while loop.

OTHER PIECES OF ADVICE I would also rename false to num_incorrect_tries, and replace 10**level-1 with 10**(level-1)

You can also just for for i in range(11), instead of doing a while loop, and incrementing i

CodePudding user response:

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")

  • Related