# Little Professor display number of problems correct

Providing the whole code i cant spot what causing this error tried already shifting etc python tutor and rewriting getting back to the same spot. Can you spot whats wrong??

``````import random

def main():
level = get_level()
live = 3
score = 0
question = 10

while question > 0 :
num1, num2, suum = generate_integer(level)
lives = live
while lives >= 1:
try:
guess = input(f"{num1} + {num2} = ")
if guess != str(suum):
print("EEE")
lives -= 1
else:
score += 1
break
except ValueError:
print("EEE")
if lives == 0:
print(f"{num1} + {num2} = {suum}")
lives = 3

question -= 1
break

print (f"Score: {score}")

def get_level():
while True:
try:
lvl = input("Level: ")
if lvl in ["1", "2", "3"]:
return lvl
else:
raise ValueError
except ValueError:
pass

def generate_integer(level):
try:
if level == "1":
num1 = random.randint(0, 8)
num2 = random.randint(0, 8)
return num1, num2, (num1 + num2)
elif level == "2":
num1 = random.randint(10,98)
num2 = random.randint(10,98)
return num1, num2, (num1 + num2)
elif level == "3":
num1 = random.randint(100,998)
num2 = random.randint(100,998)
return num1, num2, (num1 + num2)
except ValueError:
pass

if __name__ == "__main__":
main()
``````

Fixed version:

``````import random

def main():
level = get_level()
live = 3
score = 0
question = 10

while question > 0 :
num1 = generate_integer(level)
num2 = generate_integer(level)
lives = live
res = num1 + num2
while lives >= 1:
guess = input(f"{num1} + {num2} = ")
if guess != str(res):
print("EEE")
lives -= 1
else:
score += 1
break
if lives == 0:
print(f"{num1} + {num2} = {res}")
question -= 1

print (f"Score: {score}")

def get_level():
level = ""
allowed = ["1", "2", "3"]
while level not in allowed:
try:
level = input("Level: ")
except ValueError:
pass

return level

def generate_integer(level):
if level == "1":
num1 = random.randint(0, 9)
return num1
elif level == "2":
num1 = random.randint(10,99)
return num1
elif level == "3":
num1 = random.randint(100,999)
return num1
else:
raise ValueError

if __name__ == "__main__":
main()
``````

In the future please try to use check50 first to see what it tells you your errors might be and break the problem down into parts and make sure each part is working.. Does `get_level` work properly? Does `generate_integer` work properly? (Hint it does not). You said whats causing this error, what's causing what error? In the future don't just do a blind code dump. Give an actual problem and demonstrate you've actually tried to solve it.

1. You have a `break` in your main loop as soon as you finish the 1st question.. This is incorrect.

2. You accomplish nothing by settings `lives=3` at the end if the loop in the `if lives == 0` condition because when you go back to the top of the while loop you reset `lives = live`.

3. You are missing an else case in `generate_integer` to raise a ValueError. You shouldnt be catching it.

4. Your `generate_integer` is wildly incorrect. Hint: It's name is `generate_integer` not `generate_integers`. It should generate a single integer as a function of the level. That's it. From the instructions:

generate_integer returns a randomly generated non-negative integer with level digits or raises a ValueError if level is not 1, 2, or 3

That is not what your function is doing.

1. In `get_level` you have over complicated this. There is no reason for you to raise and then catch an exception there. Given that it is a condition you know is a possibility and which you desire to handle and re-loop over usage of an exception for this is overkill, bad practice and not performant. If the user doesn't provide an input that is in your selection criteria you simply need to go back around the loop and prompt them again for input. Secondly `while True` should be avoided. Instead I would strongly consider you use the input conditions as your boundary. here's an example:
``````def get_level():
level = ''
allowed = ['1', '2', '3']
while level not in allowed:
try:
level = input("Level: ")
except Exception:
pass # we dont care

# the only way out the loop is when level is one of the desired values
return level
``````

Now the only exceptions being handled are cases I truly can't predict and I have code in place for reasonably expected things. Raising exceptions is powerful and useful but should be done for things that are more unexpected generally than merely a user inputting a value not among this set particularly when you are going to reprompt them.

1. In the body of your main loop you have a section where you have a try catch to get some input and check if its right or not, but you are catching a `ValueError` however there is nothing in that section that can generate a `ValueError`. So remove that.

2. Your ranges for the number generation look wrong. Why only `0` through `8`, why not `9`? similar for the other ranges. This is incorrect. Why are you excluding numbers `9`, `99` and `999`?

• Hei thank you for such extensive answer, the error that was left unpassed was :( Little Professor displays number of problems correct expected "9", not "Level: 6 + 6 =..." That was mistake at the first place to post a question, at night after work and frustrated after pondering this few hours and rewriting it. Commented Dec 15, 2023 at 7:24
• Cannot thank you enough for that comment, especially no. 5 is great, i will try not to do those unnecessary posts before few days passes without a solution. Commented Dec 15, 2023 at 7:31
• Happy to help.. Its important to provide a thorough answer so as to explain the reasoning as the goal here isnt as much for you to solve the little professor problem as much as it is to master the material that is in the problem. Being able to ask targeted questions is a skill you will improve upon in time. I was of course not trying to imply you didn't try, the problem I imagine is you weren't sure what to try or how to try. And my goal was to partially to help cultivate those skills. Unit Testing, debugging and breaking problems into smaller problems, "this is the way" good luck! Commented Dec 15, 2023 at 15:02
• You can post as early as you are stuck, I take no issue with that. All I would say is that before you post, have a targeted problem (not just that it doesn't work), and do some basic testing ( whether its with debugger or printouts combined with usage of the program) to try and figure out where your code is 'going astray' / not doing what its being asked to do or not doing what you think it should be doing. That will serve you greatly particularly when you advance beyond this course and move to post code questions on StackOverflow.com when you are doing more general coding. Commented Dec 15, 2023 at 15:04
• Computer science is all about turning big problems into smaller problems and solving them. Following the scientific method to remove uncontrolled variables and ensuring that your smaller subsections or functions are doing what they need to do, and composing them into something larger is a big part of how to effectively and efficiently write larger and larger programs. It's why unit testing is so effective and critical. Commented Dec 15, 2023 at 15:09