如何让游戏中的得分反映词典内容?
我在处理字典的时候遇到了一些困难。目前程序输出的分数是1和2,而不是我想要的15和30,我该怎么修改才能达到这个效果呢?
import numpy as np
# This imported numpy as np
def single_game():
keep_going= True
''' This is a single game of tennis, the aim of the game
is to score 40 points'''
receiver_score = 0
server_score = 0
ace_winner_count = 0
double_fault_count = 0
service_continues_count = 0
server_dict = {
"0": 0,
"1":15,
"2":30,
"3":40,
"4": "Deuce"}
receiver_dict = {
"0": 0,
"1":15,
"2":30,
"3":40,
"4": "Deuce"}
print("Hello and welcome to our Grand Slam Game!")
print("Please roll the dice!")
while keep_going:
diceroll_yellow = np.random.randint(1, 7)
if diceroll_yellow == 1 or diceroll_yellow == 2:
print("The number you rolled is:", diceroll_yellow)
print("You are the ace winner!")
print("Points gained for server!", server_dict["1"])
server_score += 1
ace_winner_count += 1
if server_score >= 4 or (server_score >= 4 and receiver_score <= server_score - 2):
print("Server wins the game!")
break
elif diceroll_yellow == 3:
print("The number you rolled is:", diceroll_yellow)
print("Double fault")
print("Points gained for receiver!", receiver_dict["1"])
double_fault_count += 1
receiver_score += 1
if receiver_score >= 4:
print("Receiver wins the game!")
break
else:
print("The number you have rolled is:", diceroll_yellow)
print("Service good")
print("Please roll the blue dice")
service_continues_count += 1
diceroll_blue = np.random.randint(1, 7)
if diceroll_blue == 1:
print("The number you rolled is:", diceroll_blue)
print("Receiver winner! Woop Woop!!")
print("Points gained for receiver!", receiver_dict["1"])
receiver_score += 1
ace_winner_count += 1
if receiver_score >= 4 or (receiver_score >= 4 and server_score <= receiver_score - 2):
print("Receiver wins the game!")
break
elif diceroll_blue == 2:
print("The number you rolled is:", diceroll_blue)
print("Out! Server wins the point")
print("Points gained for server!", server_dict["1"])
server_score +=1
if server_score >= 4 or (server_score >= 4 and receiver_score <= server_score - 2):
print("Server wins the game!")
break
else:
print("The number you rolled is:", diceroll_blue)
print("Service continues")
print("Please roll the red dice")
service_continues_count += 1
diceroll_red = np.random.randint(1, 7)
if diceroll_red == 1:
print("The number you rolled is:", diceroll_red)
print("Server wins the point")
print("Points gained for server!", server_dict["1"])
server_score += 1
if server_score >= 4 or (server_score >= 4 and receiver_score <= server_score - 2):
print("Server wins the game!")
break
elif diceroll_red == 2:
print("The number you rolled is:", diceroll_red)
print("Receiver wins the point")
receiver_score += 1
print("Points gained for receiver!", receiver_dict["1"])
if receiver_score >= 4 or (receiver_score >= 4 and server_score <= receiver_score - 2):
print("Receiver wins the game!")
break
else:
print("The number you rolled is:", diceroll_red)
print("Service continues")
print("Please roll the blue dice")
service_continues_count += 1
if server_score >= 4 and receiver_score>=4 :
print("Deuce!")
keep_going= False
#HOW TO AMEND THE SCORES LIKE IN TENNIS
print("Number of ace count:", ace_winner_count)
print("Number of double faults:", double_fault_count)
print("Service continues count:", service_continues_count)
print("You have come to the end of the game!")
single_game()
我希望打印出来的结果是这样的:
15- 30
而不是1和2。
1 个回答
1
我没有打过网球,所以对这个游戏的逻辑不太了解。不过,我对你的代码做了一些修改,改变了你从字典中获取值的方式。你之前是这样做的:
server_dict["1"]
但你应该这样做:
server_dict[str(server_score)]
因为你在这些变量中存储和更新分数,我建议你把代码分成几个模块。这样做会让你更容易理清逻辑流程,并使用调试工具实时查看代码每一步发生了什么。你甚至可以看到哪些值被存储了,以及为什么会这样:
import numpy as np
# This imported numpy as np
def single_game():
keep_going= True
''' This is a single game of tennis, the aim of the game
is to score 40 points'''
receiver_score = 0
server_score = 0
ace_winner_count = 0
double_fault_count = 0
service_continues_count = 0
server_dict = {
"0": 0,
"1":15,
"2":30,
"3":40,
"4": "Deuce"}
receiver_dict = {
"0": 0,
"1":15,
"2":30,
"3":40,
"4": "Deuce"}
print("Hello and welcome to our Grand Slam Game!")
print("Please roll the dice!")
while keep_going:
diceroll_yellow = np.random.randint(1, 7)
print("\n")
if diceroll_yellow == 1 or diceroll_yellow == 2:
server_score += 1
ace_winner_count += 1
print("The number you rolled is:", diceroll_yellow)
print("You are the ace winner!")
print("Points gained for server!", server_dict[str(server_score)])
if server_score >= 4 or (server_score >= 4 and receiver_score <= server_score - 1):
print("Server wins the game!")
break
elif diceroll_yellow == 3:
double_fault_count += 1
receiver_score += 1
print("The number you rolled is:", diceroll_yellow)
print("Double fault")
print("Points gained for receiver!", receiver_dict[str(receiver_score)])
if receiver_score >= 4:
print("Receiver wins the game!")
break
else:
print("The number you have rolled is:", diceroll_yellow)
print("Service good")
print("Please roll the blue dice")
service_continues_count += 1
diceroll_blue = np.random.randint(1, 7)
if diceroll_blue == 1:
receiver_score += 1
ace_winner_count += 1
print("The number you rolled is:", diceroll_blue)
print("Receiver winner! Woop Woop!!")
print("Points gained for receiver!", receiver_dict[str(receiver_score)])
if receiver_score >= 4 or (receiver_score >= 4 and server_score <= receiver_score - 2):
print("Receiver wins the game!")
break
elif diceroll_blue == 2:
server_score +=1
print("The number you rolled is:", diceroll_blue)
print("Out! Server wins the point")
print("Points gained for server!", server_dict[str(server_score)])
if server_score >= 4 or (server_score >= 4 and receiver_score <= server_score - 2):
print("Server wins the game!")
break
else:
print("The number you rolled is:", diceroll_blue)
print("Service continues")
print("Please roll the red dice")
service_continues_count += 1
diceroll_red = np.random.randint(1, 7)
if diceroll_red == 1:
server_score += 1
print("The number you rolled is:", diceroll_red)
print("Server wins the point")
print("Points gained for server!", server_dict[str(server_score)])
if server_score >= 4 or (server_score >= 4 and receiver_score <= server_score - 2):
print("Server wins the game!")
break
elif diceroll_red == 2:
print("The number you rolled is:", diceroll_red)
print("Receiver wins the point")
receiver_score += 1
print("Points gained for receiver!", receiver_dict[str(receiver_score)])
if receiver_score >= 4 or (receiver_score >= 4 and server_score <= receiver_score - 2):
print("Receiver wins the game!")
break
else:
print("The number you rolled is:", diceroll_red)
print("Service continues")
print("Please roll the blue dice")
service_continues_count += 1
if server_score >= 4 and receiver_score>=4 :
print("Deuce!")
keep_going= False
#HOW TO AMEND THE SCORES LIKE IN TENNIS
print("Number of ace count:", ace_winner_count)
print("Number of double faults:", double_fault_count)
print("Service continues count:", service_continues_count)
print("You have come to the end of the game!")
single_game()