Python while循环,我被困了! :C

时间:2015-03-07 07:58:50

标签: python python-3.x while-loop

我是编程新手(刚开始3天前),我需要这段代码的帮助,我确实这样做了。

我不知道如何进行while循环。 (在####行以下)。你能救我吗?

我正在使用python版本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

1 个答案:

答案 0 :(得分:0)

以下是该问题的快速解决方法:

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;

基本上,您只需要将整个代码放入while循环中,以便实际重复。

另外,我真的希望当我将"y"放在引号中以使其成为字符串时,我并没有陷入困境。我已经超过一个小时没碰到Python了,所以我对语法感到朦胧。