我必须编写一个代码,为2个玩家生成一个数字,然后,如果在2个玩家之间得分最高的玩家将获得一个积分。第一个获得10分胜利的玩家,该程序应打印出谁获胜,并计算得出的总得分数字。我无法获取要更新的变量。该程序也运行得太快,我不确定如何将其转到用户必须按Enter才能开始新游戏的地方。
ive尝试将def main():
与全局变量和return
一起使用,但它们都不起作用。
import random
player_one =input("Enter player 1's name: ").title()
player_two =input("Enter player 2's name: ").title()
total_score1 = 0
total_score2 = 0
for x in range(1,13):
player_1 = random.randint(1, 13)
player_2 = random.randint(1, 13)
if player_1 > player_2:
total_score1 = total_score1 + 1
else:
player_1 < player_2
total_score2 = total_score2 + 1
print(player_one,'\t',player_1,'\t',player_two,'\t',player_2)
if player_1 == player_2:
print("Both numbers were equal")
if total_score1 > total_score2:
print(player_one, "Wins, total points:",total_score1)
else:
print(player_two, "Wins, total points:", total_score2)
预期产量:
Payer 1 wins, total points:
变量未更新
答案 0 :(得分:0)
这有效!我更改了if else使其包含和elif,使代码更简洁。
编辑:暂时改变以10点抽泣游戏,并因睡眠而放慢速度。
import random
import time
player_one ='j'
player_two ='t'
total_score1 = 0
total_score2 = 0
while total_score1 < 10 and total_score2 < 10:
player_1 = random.randint(1, 13)
player_2 = random.randint(1, 13)
if player_1 > player_2:
total_score1 = total_score1 + 1
print(player_one,'\t',player_1)
time.sleep(1)
elif player_1 < player_2:
total_score2 = total_score2 + 1
print(player_two,'\t',player_2)
time.sleep(1)
else:
print("Both numbers were equal")
time.sleep(1)
if total_score1 > total_score2:
print(player_one, "Wins, total points:",total_score1)
else:
print(player_two, "Wins, total points:", total_score2)
输出:
('j', '\t', 12)
('j', '\t', 10)
('t', '\t', 13)
('t', '\t', 13)
('j', '\t', 10)
('t', '\t', 8)
('t', '\t', 5)
('j', '\t', 8)
('j', '\t', 8)
Both numbers were equal
Both numbers were equal
('j', '\t', 13)
('t', '\t', 4)
('t', '\t', 3)
('j', '\t', 13)
('j', '\t', 13)
('t', '\t', 13)
('t', '\t', 9)
('j', '\t', 12)
('t', '\t', 9)
('t', '\t', 12)
('t', 'Wins, total points:', 10)