我对反映字典的答案有困难。目前打印分数为 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
我没有打过网球,所以我不明白游戏的逻辑,但是,我修改了你的代码,改变了从字典中检索值的方式,你正在这样做:
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()
祝你好运,我的朋友。