Python Quiz, как мне распечатать этот тест / функции? - PullRequest
0 голосов
/ 04 сентября 2018

Я сделал анкету или короткую и простую математическую викторину для развлечения с небольшой помощью других форумов. У меня есть вопросы как функции, с ответами внутри них, например:

def q1():
       print("What is 6 divided by 2?")
       answer = str(input())
       if answer == "3":
           print("You have {} lives left".format(lives)) # displays the current lives (does not lose a life)
           print("CORRECT!")
       else:
           lives -= 1 # loses a life
           print("You have {} lives left".format(lives)) # displays the current lives (loses a life)
           print("WRONG!")
       q1()

У меня есть четыре из этих вопросов, и они все в одной функции:

def questions():
   def q1():
       print("What is 6 divided by 2?")
       answer = str(input())
       if answer == "3":
           print("You have {} lives left".format(lives)) # displays the current lives (does not lose a life)
           print("CORRECT!")
       else:
           lives -= 1 # loses a life
           print("You have {} lives left".format(lives)) # displays the current lives (loses a life)
           print("WRONG!")
       q1()
       time.sleep(2)
       print()
   def q2():
       print("What is 6 multiplied by 2?")
       answer = str(input())
       if answer == "12":
           print("You have {} lives left".format(lives)) # displays the current lives (does not lose a life)
           print("CORRECT!")
       else:
           lives -= 1 # loses a life
           print("You have {} lives left".format(lives)) # displays the current lives (loses a life)
           print("WRONG!")
       q2()
       time.sleep(2)
       print()
   def q3():
       print("What is 5 multiplied by 5?")
       answer = str(input())
       if answer == "12":
           print("You have {} lives left".format(lives)) # displays the current lives (does not lose a life)
           print("CORRECT!")
       else:
           lives -= 1 # loses a life
           print("You have {} lives left".format(lives)) # displays the current lives (loses a life)
           print("WRONG!")
       q3()
       time.sleep(2)
       print()
   def q4():
       print("What is 20 divided by 2?")
       answer = str(input())
       if answer == "12":
           print("You have {} lives left".format(lives)) # displays the current lives (does not lose a life)
           print("CORRECT!")
       else:
           lives -= 1 # loses a life
           print("You have {} lives left".format(lives)) # displays the current lives (loses a life)
           print("WRONG!")
       q4()
questions()

Мой полный код ^ выше ^ Когда я открываю интерактивное окно python, на экране ничего не отображается и не печатается.

Как мне напечатать вопросы определения и напечатать, если игрок ответил правильно или неправильно?

1 Ответ

0 голосов
/ 04 сентября 2018

Вы бы оставили функции от q1 до q4 вне функции questions, а внутри функции вопросов просто наберите:

def questions():
    q1()
    q2()
    q3()
    q4()

, а затем внизу страницы вы назовете questions(), как вы это уже делаете. Вся программа будет выглядеть примерно так:

def q1():
    print("What is 6 divided by 2?")
    answer = str(input())
    if answer == "3":
        print("You have {} lives left".format(lives))  # displays the current lives (does not lose a life)
        print("CORRECT!")
    else:
        lives -= 1  # loses a life
        print("You have {} lives left".format(lives))  # displays the current lives (loses a life)
        print("WRONG!")

def q2():
    print("What is 6 multiplied by 2?")
    answer = str(input())
    if answer == "12":
        print("You have {} lives left".format(lives))  # displays the current lives (does not lose a life)
        print("CORRECT!")
    else:
        lives -= 1  # loses a life
        print("You have {} lives left".format(lives))  # displays the current lives (loses a life)
        print("WRONG!")


def q3():
    print("What is 5 multiplied by 5?")
    answer = str(input())
    if answer == "12":
        print("You have {} lives left".format(lives))  # displays the current lives (does not lose a life)
        print("CORRECT!")
    else:
        lives -= 1  # loses a life
        print("You have {} lives left".format(lives))  # displays the current lives (loses a life)
        print("WRONG!")


def q4():
    print("What is 20 divided by 2?")
    answer = str(input())
    if answer == "12":
        print("You have {} lives left".format(lives))  # displays the current lives (does not lose a life)
        print("CORRECT!")
    else:
        lives -= 1  # loses a life
        print("You have {} lives left".format(lives))  # displays the current lives (loses a life)
        print("WRONG!")

def questions():
        q1()
        time.sleep(2)
        q2()
        time.sleep(2)
        q3()
        time.sleep(2)
        q4()

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