在其他论坛的帮助下,我进行了问卷调查或简短而简单的数学测验,很有趣。 我有一些问题,例如函数:
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交互式窗口时,屏幕上没有显示或打印任何内容。
答案 0 :(得分:0)
您可以将函数q1
到q4
留在questions
函数之外,而在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()