# Problem Set 4 Little Professor ,expected output doesn't show

I'm solving this problem enter link description here

My code doesn't pass automated test enter link description here

``````import sys
import random
def main():

get_level()

def get_level():

while True:
try:
lev=int(input("Level: ",))
if lev == 1 or lev ==2 or lev == 3:
generate_integer(lev)

except ValueError:
pass
def generate_integer(level):

if level == 1 or level == 2 or level == 3:
m=0
n=0
p=0
i=0
while i<=9 :
i=i+1
if level == 1:
a=random.randint(0,9)
b=random.randint(0,9)
elif level == 2:
a=random.randint(10,99)
b=random.randint(10,99)
else:
a=random.randint(100,999)
b=random.randint(100,999)

total= a + b
c = a
d = b
try:
guess=int(input(f"{a} + {b} = ", ))
x=str(guess)
if guess != total or x.isdigit() == False :
#n=n+1
raise ValueError
except ValueError:
n=n+1
print("EEE")
p= p+1
for j in range(1):

try:
guess=int(input(f"{a} + {b} = ", ))
x=str(guess)
if guess == total:
break
elif guess != total or x.isdigit() == False:
raise ValueError
#if guess == total:
#print(f"{a} + {b} = {total} ")
#break

except ValueError:
print("EEE")
p= p+1
for k in range(1):
try:
guess=int(input(f"{a} + {b} = ", ))
x=str(guess)
if guess == total:
break
elif guess != total or x.isdigit() == False:
raise ValueError
#elif guess == total:
#print(f"{a} + {b} = {total} ")
#break

except ValueError:
print("EEE")
p= p+1
print(f"{a} + {b} = {total} ")
break
if total == guess:
m=m+1
continue
elif total != guess or p==1 or p==2 or p==3:
n=n-1
continue
print("Score:",m - n)
sys.exit(0)

if __name__ == "__main__":
main()
``````
• @UpAndAdam the question i tried to enter was my first query in the platform so i didn't know the format to simplify. i tried to simplify the code further and still the program isn't showing expected output.The URL i provided contains (cs50 set 4 little professor) expected output of the CS50 python programming course Commented Sep 14, 2023 at 5:58
• this is better but those links are not useful. Specifically what test is failing. What is the expected input and output for that test? Have you tried running it locally? When you ran locally did it behave properly? Again please see my note on formatting your code so that all the code is formatted as code. "My code doesnt pass automated test" does not tell us anything to help you with. Commented Sep 14, 2023 at 14:50
• for starters if you are checking that `level` is valid before calling into `generate_integer` you don't have to check `level` again.. check it on one place is a best practice. not going to fix your code but will improve your code Commented Sep 14, 2023 at 14:52
• Your code is needlessly complex and you are throwing exceptions when you really should not be throwing them. For each of the ten problems just have a loop for up to 3 tries after which you reveal the answer. Follow the instructions of that link on "How to test" where do you get when you do that? What works and what doesn't work? Commented Sep 14, 2023 at 14:56

This won't solve all of your problems but let's start getting you in the right direction

First thing i will address is that your functions are not doing what they are supposed to. It is important to follow instructions and make functions do what they are supposed to do. This becomes more and more critical as you build larger pieces of software and when you work with other people. What if you are were responsible for `get_level` and I was responsible for `generate_integer`? or what if you were responsible for both and I was responsible for the `main` code? Think of function specifications kind of like contracts, users of the function expect it to do a certain thing, and only that certain thing that its advertised to do.

• `get_level` is only supposed to prompt and reprompt the user for a value from 1-3 and return it. Your code doesn't reprompt if the answer isn't valid and calls `generate_integer` which it should not. Both of those things are bad.

Here is an example of a valid `get_level` implementation

``````def get_level():
level = 0
while level not in [1, 2, 3]:
try:
level = int(input(Level: ))
except:
level = 0
return level
``````
• `generate_integer` is only supposed to generate a random non-integer with `level` digits in it. You basically have the bits needed here you just have other things co-mingled into it.

Let's fix that:

``````def generate_integer(level):
number = 0
if level == 1:
number = random.randint(0,9)
elif level == 2:
number = random.randint(10,99)
else:
number = random.randint(100,999)

return number
``````

An important note here is that now you can perform unit testing on those functions to make sure they are doing what they should without having to sift through the whole program. This is a fundamental piece of good code design especially when you are working on less complex projects to make them easy to test.

Ok now we have some basics down.

Next thing we need to do is write a function to handle asking a single question... let's assume we do that and it returns `1` if they got it right and `0` if they got it wrong.

now our `main` can be:

``````def main():
level = get_level()
score = 0
for i in range(9):
score += handle_problem(generate_integer(level), generate_integer(level))
print("Score: ", str(score))
``````

Now all you have to do is fill in a definition of `handle_problem` you have much of the code but it should be simpler. You just need to do something and i'd suggest an outer loop of `for i in range 2` which will let you try the loop body 3 times. Remember you can break out of a loop if you find the condition that you need met with `break` and you can also introduce a variable if you wanted to to let you know if the answer was indeed right or not. The basics are that you will have a loop in which they are given the chance to make a guess and either its wrong and you print the `EEE` and try again until the loop ends or if its right you can break out of the loop, then handle if its right returning 1, and if it was wrong printing the correct answer and returning 0.