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:
JavaScript
x
19
19
1
fals = 0
2
while fals < 10:
3
if level == 1:
4
x = random.randint(0,9)
5
y = random.randint(0,9)
6
elif level == 2:
7
x = random.randint(10,99)
8
y = random.randint(10,99)
9
else: # Next convert to else
10
x = random.randint(100,999)
11
y = random.randint(100,999)
12
answer = x + y
13
14
while (guess := input(f'{x} + {y} = ')) != answer:
15
fals += 1
16
if fals == 3:
17
print(f'{x} + {y} = {answer}')
18
break
19
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
JavaScript
1
26
26
1
import random
2
fals = 1
3
while fals <= 10:
4
level = int(input(str(fals)+". Enter level :"))
5
if level == 1:
6
x = random.randint(0,9)
7
y = random.randint(0,9)
8
elif level == 2:
9
x = random.randint(10,99)
10
y = random.randint(10,99)
11
else: # Next convert to else
12
x = random.randint(100,999)
13
y = random.randint(100,999)
14
answer = x + y
15
16
i = 0
17
while i < 3:
18
guess = input(f'{x} + {y} = ')
19
if int(guess) == answer:
20
print("Correct")
21
break
22
i += 1
23
if i == 3:
24
print("Answer ",answer)
25
fals+=1
26
Hope this code works.