Google0593 Google0593 - 2 months ago 7
Python Question

Python while loop, I'm stuck help! :C

I'm new to programming (just started 3 days ago) and I need help with this code, I really do.

I don't know how to do the

while
loop. (below the
####
line). Can you help me?

I'm using
python
version 3.4.2

import random

right = 0
wrong = 0

for i in range(10):

x = random.randint (2,29)
y = random.randint (3,29)
z = (x*y)
qq = input("What's " + str(x) + " times " + str(y) + "? ")


if str(z) == str(qq):
right = right +1
print ("Correct!\n")

else:
wrong = wrong + 1
print ("Wrong the correct answer is ",str(z),"\n")


print ("You got", right, "out of 10 questions")

if right==10:
print ("Well Done! Perfect score!")

elif right>=5:
print ("Well done! try getting perfect score next time")

else:
print ("Noob")
#################################################
restart = y
while True:
yess = input ("Would you like to try again? y/n?")
if restart == yess:
print (i)
else:
break

Answer

Here is the quick fix for that problem:

import random

while True:    
    right = 0
    wrong = 0

    for i in range(10):

            x = random.randint (2,29)
            y = random.randint (3,29)
            z = (x*y)
            qq = input("What's " + str(x) + " times " + str(y) + "? ")


            if   str(z) == str(qq):
                 right = right +1
                 print ("Correct!\n")

            else:
                wrong = wrong + 1
                print ("Wrong the correct answer is ",str(z),"\n")


    print ("You got", right, "out of 10 questions")

    if right==10:
            print ("Well Done! Perfect score!")

    elif right>=5:
            print ("Well done! try getting perfect score next time")

    else:
            print ("Noob")
    #################################################
    restart = "y"
    yess = input ("Would you like to try again? y/n?")
    if restart != yess:
        break;

Essentially, you just need to put your entire code into the while loop so it actually repeats.

Also, I honestly hope I didn't mess up when I put the "y" in quotation marks to make it a string. I haven't touched Python for more than an hour, so I'm hazy on the syntax.

Comments