0

在其他论坛的帮助下,我制作了一份问卷或简短而简单的数学测验。我有问题作为函数,它们的答案在其中,例如:

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交互窗口时,屏幕上没有显示或打印任何内容。

如果玩家回答正确或不正确,我将如何打印 def 问题并打印?

4

1 回答 1

0

您可以将函数留在函数q1外部q4questions而在 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()
于 2018-09-03T21:40:26.703 回答