Question:
I could use some python help? while loops?
Tessa
2012-12-12 07:26:35 UTC
I want this to repeat itself but change the values of the numbers so that it prints a different equation every time it's repeated
there's more code but it's basically the same just subt, div, and mult...

import random
#list variables
num1 = random.randint(1, 50)
num2 = random.randint(1, 50)
num3 = random.randrange(2, 200, 2)
num4 = random.randrange(2, 20, 2)
add = num1 + num2 + num3
subt = num1 - num2
mult = num1 * num4
div = num3 / num4



print """Welcome To Math Training!
Here, you will practice your basic math skills, addition, subtraction and multiplication, by solving a few equations!
So let's get started!"""

raw_input("\n\nPress the enter key to start the game.")

while True:#repeat the program
#addition
print "Solve:", num1, "+", num2, "+", num3, "="#show the equation
guess = float(raw_input("What's your answer? "))#ask the user for the solution to the problem
if guess == add:
print "Good Job! You got it."#congratulate the user if the answer is right
else:
if guess != add:
print "Wrong answer! Try again."#tell the user the answer is wrong and the game is over
guess = float(raw_input("What's your answer? "))
if guess == add:
print "Good Job! You got it."
else:
print "Wrong answer! You failed."
print "The correct answer was", add#print the right answer


PLEASE THIS IS URGENT! I'LL GIVE 5STARS IF YOU HELP ME :3
thanks
Three answers:
Francis
2012-12-12 18:54:53 UTC
yep, put the randomization of numbers inside the loop like this..



while True:#repeat the program

~~~~num1 = random.randint(1, 50)

~~~~num2 = random.randint(1, 50)

~~~~num3 = random.randrange(2, 200, 2)

~~~~num4 = random.randrange(2, 20, 2)

~~~~add = num1 + num2 + num3

~~~~subt = num1 - num2

~~~~mult = num1 * num4

~~~~div = num3 / num4

~~~~#then put the rest of your code here, addition, etc..
Lesus
2012-12-12 07:36:43 UTC
If you want the numbers to be different every time, you have to set the variables inside the loop.
anonymous
2016-12-08 17:24:35 UTC
mutually as enter != 'a' and enter != 'b' and enter != 'c' etc. yet does it no longer happen to you that your concern is purely too long winded and consequently might desire to correctly be incorrect? attempt mutually as enter < 'a' or enter > 'z' have relaxing.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...