Python测验,如何打印此测验/函数?
在其他论坛的帮助下,我做了一份调查问卷或简单的数学测验。 我将问题作为函数,其中包含答案,例如:Python测验,如何打印此测验/函数?,python,function,printing,Python,Function,Printing,在其他论坛的帮助下,我做了一份调查问卷或简单的数学测验。 我将问题作为函数,其中包含答案,例如: 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 l
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问题?您将把函数
q1
到q4
放在问题
函数之外,而在问题函数里面您只需键入:
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()
您对
q1
的唯一调用位于q1
内。也许您希望函数调用位于它们调用的函数之后,而不是在它们内部。您可能需要将调用从q1()
到q4()
取消一级。