代码如下。我想弄清楚如何让用户输入“是”'或者没有'再玩一次。我尝试通过将每条线向前间隔而将问题部分变成def,但是它仍然保持在预期的缩进线中。错误信息。是否可以在不使用defs的情况下循环整个测验
import time
#Welcome message
print("Welcome! Have a go at this quiz!")
time.sleep(1)
print("Whats your name?")
player_name = input()
#Questions
score = 0 #this defines variable score and sets it to 0
print("What is 2 + 2 " + player_name)
answer = input()
answer = int(answer)
if answer == 4:
print("Well done " + player_name)
score = score + 1 #This increases score by 1
else:
print("Sorry the answer was 4 " + player_name)
print("your score is " + str(score))
print("What is 4 + 3 " + player_name)
answer = input()
answer = int(answer)
if answer == 7:
print("Well done " + player_name)
score = score + 1 #This increases score by 1
else:
print("Sorry the answer was 7 " + player_name)
print("your score is " + str(score))
print("What is 5 + 7 " + player_name)
answer = input()
answer = int(answer)
if answer == 12:
print("Well done " + player_name)
score = score + 1 #This increases score by 1
else:
print("Sorry the answer was 12 " + player_name)
print("your score is " + str(score))
print("What is 8 + 9 " + player_name)
answer = input()
answer = int(answer)
if answer == 17:
print("Well done " + player_name)
score = score + 1 #This increases score by 1
else:
print("Sorry the answer was 17 " + player_name)
print("your score is " + str(score))
print("What is 11 + 9 " + player_name)
answer = input()
answer = int(answer)
if answer == 20:
print("Well done " + player_name)
score = score + 1 #This increases score by 1
else:
print("Sorry the answer was 20 " + player_name)
print("your score is " + str(score))
print("What is 12 + 14 " + player_name)
answer = input()
answer = int(answer)
if answer == 26:
print("Well done " + player_name)
score = score + 1 #This increases score by 1
else:
print("Sorry the answer was 26 " + player_name)
print("your score is " + str(score))
print("What is 15 + 15 " + player_name)
answer = input()
answer = int(answer)
if answer == 30:
print("Well done " + player_name)
score = score + 1 #This increases score by 1
else:
print("Sorry the answer was 30 " + player_name)
print("CONGRATULATIONS!, You have finished this quiz!")
print("your final score was " + str(score))
if score > 6:
print("Great work! You got top marks!")
elif score > 5:
print("Good, try harder next time and you might get full marks!")
elif score > 4:
print("You did alright")
elif score > 3:
print("That was ok")
elif score > 2:
print("Ummm...Maybe next time you should know study or try harder")
else:
print("Wow, You are horrible at math...")
答案 0 :(得分:1)
如果将代码分解为函数,则更容易控制代码流,例如:
让我们做一个函数,其目的是提出一个问题并确定它们是对还是错
def askQuestion():
import random
num1 = random.randint(1,20)
num2 = random.randint(1,20)
answer = int(input('What is {} + {}'.format(num1, num2)))
if (num1 + num2) == answer:
print('Well done')
return 1
else:
print('Sorry the answer was {}'.format(answer))
return 0
现在,让我们定义另一个评估最终得分的函数
def assessScore(score):
if score > 6:
print("Great work! You got top marks!")
elif score > 5:
print("Good, try harder next time and you might get full marks!")
elif score > 4:
print("You did alright")
elif score > 3:
print("That was ok")
elif score > 2:
print("Ummm...Maybe next time you should know study or try harder")
else:
print("Wow, You are horrible at math...")
最后,我们可以定义我们的main
函数,您可以根据需要调用其他函数
def main():
total = 0
for question in range(6):
total += askQuestion() # Note we called askQuestion here
assessScore(total) # and now we called assessScore
由于我们将代码分解为函数,因此我们可以根据需要多次调用askQuestion
。如果我们想重新循环整个程序,我们现在甚至可以这样做
def main():
keepPlaying = True
while keepPlaying:
total = 0
for question in range(6):
total += askQuestion() # Note we called askQuestion here
assessScore(total) # and now we called assessScore
answer = input('Would you like to play again? (Y or N)')
keepPlaying = answer in ('Y', 'y')