Computer science 在python代码中保持分数
因为我需要记分板,所以我必须做什么才能为我的代码设置记分板。我试了一会儿,但还是迷路了。你能帮助我吗?太迷路了Computer science 在python代码中保持分数,computer-science,python-3.4,Computer Science,Python 3.4,因为我需要记分板,所以我必须做什么才能为我的代码设置记分板。我试了一会儿,但还是迷路了。你能帮助我吗?太迷路了 import random def main_menu(): option = input("Play the game (P) , View the game credits (V), or Quit (Q)") if (option == "Play the game") or (option == "P"): riddles = [
import random
def main_menu():
option = input("Play the game (P) , View the game credits (V), or Quit (Q)")
if (option == "Play the game") or (option == "P"):
riddles = [
{"riddle": "What walks on four legs in the morning, two legs in the afternoon, and three in the evening?",
"answer": ["Monkey", "Humans", "Nothing Silly"],
"correct": "2"},
{"riddle": "What has roots as nobady sees?",
"answer": ["River", "Famliy Tree", "Mountain"],
"correct": "3"},
{"riddle": " I am the ruin of men, and yet they lust for me. \
I have no power, no strength, and yet I am the might of kings and armies. \
I am hard as dragons scales, yet I flow like water. \
Men dream of me, and yet once found I am cast aside. \
I am a dancing rainbow, ever chased, never caught.",
"answer": ["Gold", "Sex", "Mountain"],
"correct": "1"}]
print("Welcome to the Hell of Riddles!!!!")
print("Hope you can think")
random.shuffle(riddles)
for riddle in riddles:
print (riddle["riddle"])
for i, choice in enumerate(riddle["answer"]):
print(str(i + 1) + ". " + choice)
answer = input("Choose the number of the answer:")
if answer == riddle["correct"]:
print("Congratz! You can think!!!")
else:
print("Not correct... Pathetic and disappointing.")
elif (option == "View the game credits") or (option == "V"):
print("This game is brought to you by: Hailey Reisner and Ashleigh Woodard")
elif (option == "Quit") or (option == "Q"):
print("QUITER")
print(main_menu())
创建一个计数器并添加到其中以获得正确答案。您还可以为错误答案创建计数器 当然,您首先要声明这些变量 因此,递增计数器的代码如下:rightAnswer+=1
if answer == riddle["correct"]:
print("Congratz! You can think!!!")
rightAnswer+=1
else:
print("Not correct... Pathetic and disappointing.")
wrongAnswer+=1
print('Right: %d\tWrong: %d) % (rightAnswer, wrongAnswer)
保留一个计数器,例如,
score=0
。现在按如下方式修改代码:
if answer == riddle["correct"]:
print("Congratz! You can think!!!")
score++
else:
print("Not correct... Pathetic and disappointing.")
score--
现在,你需要增加更多的复杂因素,比如分数是否为负值等等