0

my program seems to be working appropriately while completing the problem's requirements.

I don't know if my solution is just outright wrong or what is happening really.

Here's my code :

#prompt user for lvl between 1,2,3. If not one of those 3, prompt again -> done
#generate 10 random addition problems -> done
#each operator should be positive number+done
#prompts user for their solution to each pb-> done
#allows 3 errors per pb -> done
#after each error display EEE and reprompt->done
#if errors>3 for a single pb, show solution->done
#ultimately ouput total score of user out of 10->done

import random
def main() :
    generate_integer(get_level())

def get_level():
    while True:
        x= input("choose lvl ")
        ok_lvl = ["1","2","3"]
        if x in ok_lvl:
            return x
        else :
            continue


def generate_integer(level):
    start=0
    score = 10
    while start<10:
            liste = list()
            for i in range (2):
                n = round(random.uniform(1,10), 2)

                if level =="1":
                    liste.append(round(n))
                else :
                    num_level=int(level)
                    n=(round(n * 10**(num_level-1)))
                    liste.append(n)

            result = liste[0] + liste[1]
            while True:
                attempt = 0
                while attempt<3 :
                    user_answer = input(f"{liste[0]} + {liste[1]} =")
                    if int(user_answer) == result:
                        start += 1
                        break
                    else:
                        print("EEE")
                        attempt= attempt +1
                        continue
                break
            if attempt==3:
                print(f"{liste[0]} + {liste[1]} = {result}")

                score-=1
                start+=1
    print(score)

if __name__=="__main__":
    main()

Thanks a lot for the help !

2 Answers 2

1

Your code structure doesn't match the instructions. They state: "generate_integer() returns a randomly generated non-negative integer with level digits or raises a ValueError if level is not 1, 2, or 3". Your generate_integer() does all of the math and doesn't have a return. Instead, you need to reorganize your code to call it from main() and return values as described above, then check the answers and re-prompt as needed.
Also, while you are working on this, pay close attention to this requirement (hidden in "How to Test" under bullet 3): Your program should output 10 distinct problems... In other words, it should not prompt with the same 2 values twice.

1
  • thanks a lot for this detailed answer Commented Oct 27, 2022 at 8:21
0
import random
import sys

def main():
    level = get_level()
    win = 0
    for _ in range(10):
        x = generate_integer(level)
        y = generate_integer(level)
        win = play(x,y,win)
    print(f'Score: {win}')
    sys.exit()

def play(x,y,win):
    res = x + y
    for i in range(3):
        r = int(input(f'{x} + {y} = '))
        if r == res and i == 0:
            win += 1
            break
        elif r == res:
            break
        else:
            print('EEE')
            if i == 2:
                print(f'{x} + {y} =', res)
    return win
def get_level():
    while True:
        try:
            level = int(input('Level: '))
            if level not in [1,2,3]:
                continue
            return level
        except:
            pass

def generate_integer(digits):
    try:
        if digits not in [1,2,3]:
            raise ValueError
        if digits == 1:
            num = random.randint(0,9)
        else:
            num = random.randint(10**(digits-1),(10**digits)-1)
        return num
    except ValueError:
        print('Enter Valid Level')

if __name__ == "__main__":
    main()

You must log in to answer this question.

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