Skip to content
Advertisement

how to make a guess number game?

im trying to make a guessing game, user can input the level she wants, if 1 just numbers between 1,9 and if 2 between 10,99 otherwise between 100,99… here is my code:

fals = 0
while fals < 10:
    if level == 1:
        x = random.randint(0,9)
        y = random.randint(0,9)
    elif level == 2:
        x = random.randint(10,99)
        y = random.randint(10,99)
    else: # Next convert to else
        x = random.randint(100,999)
        y = random.randint(100,999)
    answer = x + y

    while (guess := input(f'{x} + {y} = ')) != answer:
        fals += 1
        if fals == 3:
            print(f'{x} + {y} = {answer}')
            break

problem is when i input 1 or 2 or 3 it always returns a 3digit question like 395 + 341 — anyway the other problem is i want to ask 10 questions and also for each question if user inputs the answer 3 time false, program will give the user correct answer and keep asking other question

Advertisement

Answer

import random
fals = 1
while fals <= 10:
    level = int(input(str(fals)+". Enter level :"))
    if level == 1:
        x = random.randint(0,9)
        y = random.randint(0,9)
    elif level == 2:
        x = random.randint(10,99)
        y = random.randint(10,99)
    else: # Next convert to else
        x = random.randint(100,999)
        y = random.randint(100,999)
    answer = x + y

    i = 0
    while i < 3:
        guess = input(f'{x} + {y} = ')
        if int(guess) == answer:
            print("Correct")
            break
        i += 1
        if i == 3:
            print("Answer ",answer)
   fals+=1

Hope this code works.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement