我的代码基本上是一个掷骰子游戏,但是最后,该代码旨在说明玩家1击败玩家2多远,但是代码输出的随机整数我认为与任何东西都不相关
说实话,我不知道该怎么做,我已经看过几次代码了,我感到困惑。
import random
Round = int(input("Developer use only, set to 5 otherwise "))
p1_extra = 0
p1_total = 0
p1_turns = 5
p2_extra = 0
p2_total = 0
p2_turns = 5
for i in range (Round):
p1_go = input("Please type 'ROll' to roll the dice, Player One ")
while p1_go != ("ROLL"):
p1_go = input("Please type 'Roll' to roll the dice. (caps sensitive)")
p1_roll_1 = random.randint(1,6)
p1_roll_2 = random.randint(1,6)
p1_add = (p1_roll_1) + (p1_roll_2)
p1_total = p1_add + p1_total
print ("P1: On your first roll you got " , p1_roll_1 , ". On your second roll you got " , p1_roll_2 , ". In total you have " , p1_total)
if p1_total % 2 == 0:
print ("Player One Got An Extra 10 Points For Getting An Even Number! ")
p1_total = (p1_total) + 10
print ("Player Ones Total Is Now " , p1_total)
else:
print ("Player One Lost 5 Points For Getting An Odd Number! ")
p1_total = (p1_total) - 5
print ("Player ones Total Is Now " , p1_total)
if p2_total % 2==0:
print ("Player Two Got An Extra 10 Points For Getting An Even Number! ")
p1_total = (p2_total) + 10
print ("Player Twos Total Is Now " , p2_total)
else:
print ("Player Two Lost 5 Points For Getting An Odd Number! ")
p1_total = (p1_total) - 5
print ("Player Twos Total Is Now " , p2_total)
if p1_total > p2_total:
print ("Player 1 Won. Congratulations!!! ")
difference = (p1_total) - (p2_total)
print ("Player 2 was " , difference , " points away from drawing ")
else:
print ("Player 2 Won. Congratulations!!! ")
difference = (p2_total) - (p1_total)
print ("Player 1 was " , difference , " points away from drawing ")
预期结果是指一个人的最终得分与两个人的最终得分之间的差,但是代码输出的数字不同,永远不会校正预期的结果。
答案 0 :(得分:1)
我相信您的代码中有错字,并且您实际上并未正确更新p2_total
。您正在将值分配给p1_total
。尝试更改以下几行:
if p2_total % 2==0:
print ("Player Two Got An Extra 10 Points For Getting An Even Number! ")
p2_total = (p2_total) + 10 # Renamed this line
print ("Player Twos Total Is Now " , p2_total)
else:
print ("Player Two Lost 5 Points For Getting An Odd Number! ")
p2_total = (p2_total) - 5 # Renamed this line
print ("Player Twos Total Is Now " , p2_total)