-5

hello i am trying to make a code for a random maths quiz generator i have it so it randoms the number and the operations but i cant get it to repeat 10 times as i want it to ask 10 questions can someone help please here is my code

import random
import time

name=input("What is your name?")
print ("Alright",name,"Welcome to your maths quiz")
score=0
question=0
finish= False
ops = ['+', '-', '*']
rand=random.randint(1,10)
rand2=random.randint(1,10)
operation = random.choice(ops)
maths = eval(str(rand) + operation + str(rand2))
print ("Your first question is",rand,operation,rand2)
question=question+1
d=int(input ("What is your answer:"))
if d==maths:
    print ("Correct")
    score=score+1
else:
    print ("Incorrect. The actual answer is",maths)
  • https://stackoverflow.com/questions/26260950/how-can-i-randomly-choose-a-maths-operator-and-ask-recurring-maths-questions-wit/26261125#26261125 – Cory Kramer Mar 06 '15 at 15:31
  • i have tried a while loop but i dont know what im doing as im very new to python – Danny Bradshaw Mar 06 '15 at 15:31
  • 3
    You kidding me? You wrote this but you can't figure out how to repeat it 10 times?? – Omid Mar 06 '15 at 15:31
  • You can use while loop. `while question <= 10:` then rest of your code then `question=question+1` which you already did. – Tanveer Alam Mar 06 '15 at 15:32
  • `for _ in range(5): print('hi')`. – user Mar 06 '15 at 15:32
  • Use a for loop to execute the code 10 times. – Malik Brahimi Mar 06 '15 at 15:33
  • Off topic: Be careful with `eval()`. It can be very dangerous if it executes user input. (This doesnt seem to be the case in your code.. just saying). – user Mar 06 '15 at 15:35
  • okay tanveer i am trying the while question but where do i put it as if i put it before print it just prints the question 10 times and other places it misses out the question sorry for seeming so stupid i have never really coded anything before and want to learn with simple things – Danny Bradshaw Mar 06 '15 at 15:37
  • Insert **only** the things you want to be repeated in the `while` or `for` loop. – user Mar 06 '15 at 15:38
  • im still confused can someone edit into the code i supplied please im trying to use a while loop as the for loop didnt work i want it to ask a different question every time but i doesnt work no matter where i put the while question <=10: – Danny Bradshaw Mar 06 '15 at 15:43
  • `for` loop should work as well. Anyway, i edited my answer below; now it contains your code the way you (probably) wanted it to work. – user Mar 06 '15 at 15:52
  • Thank you it what i wanted thanks – Danny Bradshaw Mar 06 '15 at 15:58

2 Answers2

1

Use while loop with condition.

Algo:

  1. Set counter to 0.
  2. Use while loop and check counter less then 10 or not.
  3. Ask question to user.
  4. Do your calculation.
  5. Increase counter by one.
  6. When counter is equal to 10, that time condition will be False.

Demo:

>>> counter = 0
>>> while counter<10:
...    que = raw_input("Enter que:")
...    print que
...    counter += 1
... 
Enter que:1
1
Enter que:2
2
Enter que:3
3
Enter que:4
4
Enter que:5
5
Enter que:6
6
Enter que:7
7
Enter que:8
8
Enter que:9
9
Enter que:10
10
>>> 
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
0

Use a for loop:

for num in range(5): 
    # Replace "print" below, with the code you want to repeat.
    print(num)

To repeat all questions, excluding "whats your name.." include the part of the code you need in the loop:

import random

name=input("What is your name?")
print ("Alright",name,"Welcome to your maths quiz")
score=0

for question_num in range(1, 11):
    ops = ['+', '-', '*']
    rand=random.randint(1,10)
    rand2=random.randint(1,10)
    operation = random.choice(ops)
    maths = eval(str(rand) + operation + str(rand2))
    print('\nQuestion number: {}'.format(question_num))
    print ("The question is",rand,operation,rand2)

    d=int(input ("What is your answer:"))
    if d==maths:
        print ("Correct")
        score=score+1
    else:
        print ("Incorrect. The actual answer is",maths)
user
  • 5,370
  • 8
  • 47
  • 75