Python测验,如何打印此测验/功能?

问题描述 投票:0回答:1

在其他论坛的帮助下,我做了一个问卷调查或简短的数学测验以获得乐趣。我有问题作为功能,他们的答案在他们内部,例如:

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问题并打印?

python function printing
1个回答
0
投票

你可以在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()
© www.soinside.com 2019 - 2024. All rights reserved.