0

When doing check50 I get the error ":( Little Professor displays number of problems correct ". I've found this post where the code works but after comparing it with my own code I still can't find the reason why it won't work. I've changed the def get_level() and def get_integer() after my search but it stil doesn't work. Screenshot of the error.

import random
def main():
    score = count = wrong_count = 0

    level = get_level()

    while True:
        y = generate_integer(level)
        z = generate_integer(level)
        answer = input(f"{y} + {z} = ")
        try:
            if int(answer) == y + z:
                score += 1
                count += 1
            else:
                while True:
                    print("EEE")
                    wrong_count += 1
                    count += 1
                    if count == 10:
                        break
                    if wrong_count == 3:
                        print(f"{y} + {z} = {y+z}")
                        wrong_count = 0
                        break
                    answer = input(f"{y} + {z} = ")
                    if answer == str(y + z):
                        score += 1
                        break
        except ValueError:
            print("EEE")
        if count == 10:
            print(f"Score: {score}")
            break

def get_level():
    while True:
            level = input("level: ")
            if level in ["1", "2", "3"]:
                return level


def generate_integer(level):
    if level == "1":
        return random.randrange(0, 9)
    elif level == "2":
        return random.randrange(10, 99)
    elif level == "3":
        return random.randrange(100, 999)
    else:
        raise ValueError
if __name__ == "__main__":
    main()
2
  • Make sure to test it yourself and enter a one wrong answer (on the same problem) 3 times. Does it give the correct score? Commented Feb 11 at 15:53
  • also try entering a wrong answer followed by a correct answer, and then remaining answers correctly.. how many questions are you asked? what is your final score? You have a number of significant errors that some basic testing should reveal. which is why I'm following @DinoCoderSaurus in not just giving an answer but pointing you to test your program.
    – UpAndAdam
    Commented Feb 12 at 16:39

2 Answers 2

1

When the question is asked again for the second or third time (after answering the first wrong) and is then answered correctly, it was not considered an attempt. In other words, the 'count' variable, which keeps track of the number of questions asked, was not incremented, allowing for more than 10 questions in practice. To solve this I added count += 1 and now I only get greens from check50. Thanks for the DinoCoderSaurus and UpAndAdam for letting me think for myself.

0

when I use this loop I get the same error

n=10
    for ques in range (n-1):
        rand_1=generate_integer(usr_lvl)
        rand_2=generate_integer(usr_lvl)
        sum_val=rand_1+rand_2
        #get user answer to problem
        usr_ansr,fail_cnt=get_ans(rand_1,rand_2,sum_val)
        # print(usr_ansr,fail_cnt)
        # check if user eventually gave the correct answer
        if usr_ansr!="EEE":
            score_cnt+=1
        else:
            print(f"{rand_1} + {rand_2} = {sum_val}")

BUT if I use this one it goes away

 n=0
    while n<10:
        rand_1=generate_integer(usr_lvl)
        rand_2=generate_integer(usr_lvl)
        sum_val=rand_1+rand_2
        #get user answer to problem
        usr_ansr,fail_cnt=get_ans(rand_1,rand_2,sum_val)
        # print(usr_ansr,fail_cnt)
        # check if user eventually gave the correct answer
        if usr_ansr!="EEE":
            score_cnt+=1
        else:
            print(f"{rand_1} + {rand_2} = {sum_val}")
        n+=1

so it does not like the range?and prefers while? Anyway it worked for me but if anyone knows why? This was driving me mad :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .