错误:分配前已引用变量

时间:2018-07-04 01:46:07

标签: python function

import random
import time
print ("Welcome to the Game")
print ("You must complete the next 10 Multiplication Questions to be truly ready for the challenges of life")
print ("")
choice = input("Are you ready? Y / N: ")
print("")

def play():

    while questions != 10:
        num1 = random.randrange(9,17)
        num2 = random.randrange(6,17)
        print("What does " + str(num1) + " x " + str(num2) + " = ")
        guess1 = input("Your guess?: ")
        answer1 = (num1*num2)
        if  int(guess1) == answer1:
            print("Correct")
            time.sleep(1)
            counter = counter + 1
            questions = questions + 1
            print("")
        else:
            print("Your answer was Wrong")
            time.sleep(1)
            print("The real answer was")
            time.sleep(1)
            print (str(answer1))
            questions = questions + 1
            print("")

        if questions == 10:
            print ("You got " + str(counter) + " out of 10")
        return
play()

3 个答案:

答案 0 :(得分:0)

从现在的可用信息来看,这是因为您没有为     问题 变量

要解决此问题,只需添加     问题= 10#或您可能想要的其他值 在play()函数的开头

答案 1 :(得分:0)

您需要在questions循环之前将0变量初始化为while,并将counters变量初始化为0,并且return语句应为在while循环之外。

下面是更正的代码

import random
import time
print ("Welcome to the Game")
print ("You must complete the next 10 Multiplication Questions to be truly ready for the challenges of life")
print ("")
choice = input("Are you ready? Y / N: ")
print("")

def play():
    #initialization
    questions,counter =0,0
    while questions != 10:
        num1 = random.randrange(9,17)
        num2 = random.randrange(6,17)
        print("What does " + str(num1) + " x " + str(num2) + " = ")
        guess1 = input("Your guess?: ")
        answer1 = (num1*num2)
        if  int(guess1) == answer1:
            print("Correct")
            time.sleep(1)
            counter = counter + 1
            questions = questions + 1
            print("")
        else:
            print("Your answer was Wrong")
            time.sleep(1)
            print("The real answer was")
            time.sleep(1)
            print (str(answer1))
            questions = questions + 1
            print("")

        if questions == 10:
            print ("You got " + str(counter) + " out of 10")
    # return outside while loop
    return
play()

答案 2 :(得分:0)

适合您的示例:

#!/usr/bin/env python3.6
import time
from random import randrange


def play():
    counter = 0
    for i in range(10):
        num1 = randrange(9, 17)
        num2 = randrange(6, 17)
        print(f"What does {num1} x {num2} = ")
        guess = input("Your guess?: ")
        answer = str(num1 * num2)
        if guess == answer:
            print("Correct\n")                
            counter += 1
        else:
            print("Your answer was Wrong")
            print(f"The real answer was {answer}\n")
        time.sleep(0.5)
    print("You got " + str(counter) + " out of 10")


def main():
    print(
        "Welcome to the Game\n"
        "You must complete the next 10 Multiplication Questions "
        "to be truly ready for the challenges of life\n"
    )
    choice = input("Are you ready? Y / N: ")
    if choice.upper() == "N":
        return
    print()
    play()


if __name__ == "__main__":
    main()