【发布时间】:2022-04-22 19:54:25
【问题描述】:
所以简而言之,谁先走是随机决定的。当人类玩家轮到时,他/她可以选择保持或滚动。如果他选择掷骰子,则掷骰子并将值相加(到 turn_score),直到大于 20,然后轮到计算机。
每回合结束后,回合分数会被添加到最终分数中。如果 1 出现,则回合分数变为 0(决赛不变)。
关于我在哪里出错的任何建议?
import random
def roll_dice():
return random.randint(1,6)
players = ['Player One', 'Player Two']
scores = {'Player One': 0, 'Player Two': 0}
random.shuffle(players)
while True:
for i in players:
if i == 'Player One':
choice = input("Roll(r) or Hold(h)?: ")
turn_score = 0
final_score = 0
if (choice=='r'):
while(turn_score<=20):
dice_value = roll_dice()
turn_score += dice_value
if (dice_value==1):
turn_score = 0
print("-rolled a ",dice_value)
print("Pigged out!")
break
print("Turn score is: ",turn_score)
final_score += turn_score
print("Your final score is: ",final_score)
if (final_score>100):
break
else:
turn_score=0
print("It is " + str(i) + "'s turn.")
while(turn_score<20):
dice_value = roll_dice()
if (dice_value==1):
turn_score = 0
scores[i] +=0
print("- rolled a ",dice_value)
print("Pigged out!")
break
else:
turn_score+=dice_value
print("-rolled a ",dice_value)
scores[i] += turn_score
print("Turn score is: ",turn_score)
print('{} score: {} {} score: {}'.format(players[0], scores[players[0]], players[1], scores[players[1]]))
if scores[i]>100:
break
if scores[i]>100:
break
winner = [winner for winner in scores if scores[winner] == max(scores.values())]
print(str(winner) + " is the winner!")`
这是我得到的输出:
It is Player Twos turn.
-rolled a 3
-rolled a 5
-rolled a 5
-rolled a 5
-rolled a 4
Turn score is: 22
Player Two score: 22 Player One score: 0
Roll(r) or Hold(h)?: h
It is Player Twos turn.
- rolled a 1
Pigged out!
Turn score is: 0
Player Two score: 22 Player One score: 0
Roll(r) or Hold(h)?: r
Turn score is: 3
Your final score is: 3
Turn score is: 5
Your final score is: 8
Turn score is: 9
Your final score is: 17
Turn score is: 13
Your final score is: 30
Turn score is: 16
Your final score is: 46
Turn score is: 19
Your final score is: 65
-rolled a 1
Pigged out!
It is Player Twos turn.
- rolled a 1
Pigged out!
Turn score is: 0
Player Two score: 22 Player One score: 0
Roll(r) or Hold(h)?:
【问题讨论】:
-
您能否详细说明当前输出有什么问题?我不知道 pig 的一般规则,从你的问题中也不完全清楚预期的输出应该是什么。
-
@JennerFelton 道歉。问题在于回合分数和最终分数的相加部分。说轮到我了,我决定滚动,即按 r。它应该准确地显示它是如何显示计算机转动的。每当玩家滚动时,这很奇怪(5+3=8,但显示为 9)。
标签: python python-3.x probability dice