Ошибка: переменная указана перед присвоением - PullRequest
0 голосов
/ 04 июля 2018
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 голосов
/ 04 июля 2018

Вам необходимо инициализировать переменную 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()
0 голосов
/ 04 июля 2018

Пример для вас:

#!/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()
0 голосов
/ 04 июля 2018

Из информации, доступной на данный момент, я бы сказал, что это потому, что вы не присвоили никакого значения для вопросы переменная

Чтобы решить эту проблему, просто добавьте Вопросы = 10 # или другое значение, которое вы можете захотеть в самом начале функции play ()

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...