将分数保持在 python 代码中
Keeping Score in python code
我需要做些什么才能为我的代码创建一个记分板,因为我需要一个记分板。我尝试了 while 语句,但在那里迷路了。你能帮助我吗?好迷路。
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--
现在由您来添加更多的并发症,例如分数是否可以为负等
我需要做些什么才能为我的代码创建一个记分板,因为我需要一个记分板。我尝试了 while 语句,但在那里迷路了。你能帮助我吗?好迷路。
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--
现在由您来添加更多的并发症,例如分数是否可以为负等