'Python program that runs a game of pig with a human player and the computer

So in a nutshell, who goes first is decided randomly. When the human players turn does come up, he/she has the option to either hold or roll. If he chooses to roll, a dice is rolled and the values are added(to turn_score) till it is >20 after which it is the computers turn.

The turn score is added to the final score after every turn. If a 1 rolls up, the turn score becomes 0(the final does NOT change).

Any advice on where I am going wrong with this one?

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!")`

Here is what I am getting as an output:

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)?:


Solution 1:[1]

Ok, unless I'm missunderstanding the game, I think your issue is that you are adding the turn score to the final score on each round, instead of adding the difference between the turn score and the original final score on each round (i.e. you are giving the player points for previous rolls on that turn even if they roll a 1 on a subsequent roll).

Instead of:

        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

I think you want:

        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)
                print("Your final score is: ", final_score + turn_score)
                if final_score + turn_score > 100:
                    final_score += turn_score
                    break

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jenner Felton