I’m here with my code, here you can see it:
JavaScript
x
31
31
1
def generate_integer(level):
2
score = 0
3
i = 0
4
false = 0
5
level = int(level)
6
while i != 10:
7
8
# Choosing the numbers of digit if 1 >> 1-9 / if 2 >> 11-99 / if 3 >> 100-999
9
end = 10**level-1
10
11
# Define x and y
12
x = random.randint(0,end)
13
y = random.randint(0,end)
14
answer = x + y
15
16
# Users cal
17
user = int(input(f'{x} + {y} = '))
18
if user == answer:
19
score = score + 1
20
while user != answer:
21
false + 1
22
print('EEE')
23
user = int(input(f'{x} + {y} = '))
24
if false == 3:
25
print(f'{x} + {y} = {answer}')
26
27
28
i = i + 1
29
30
print(f'score: {score}/10')
31
Let me explain: I defined false for, if user inputs the answer 3 times and all of them for that question are false, show user the answer and continue asking
Actually this code asks 10 different math questions, this is a part of my code, I’m checking if answer is not true print(‘EEE’) and re ask it again, but if user tries 3 time and all incorrect, then I show the answer, pass that question and keep asking other questions.
If you have any ideas for re asking question, when users input was non-numerical, I’ll be thankful.
Advertisement
Answer
JavaScript
1
25
25
1
def check(user_answer,correct_answer):
2
for a chance in range(2):
3
print("Wrong answer, try again")
4
user_answer=input('User please type your answer for the question')
5
if user_answer == correct_answer:
6
return 'True' # Given the Right answer
7
else:
8
print('Again wrong answer')
9
return 'False' #Given all wrongs answers
10
11
user_answer=input('User please type your answer for the question')
12
13
correct_answer=10
14
15
if user_answer != correct_answer:
16
result=check(user_answer,correct_answer)
17
18
if result:
19
print("your answer is correct")
20
else:
21
print("your all answers were wrong, the right answer is: ",correct_answer)
22
23
else:
24
print("Perfect your answer was right in the first guess")
25