Pregunta

I have an issue with an In-Between dice game I'm writing. I had another question posted before which was answered, but I still have a problem. (Bear in mind I'm VERY new to programming in general)

Here is the output I'm getting, it involves the 'higher or lower' option that appears if the two dice roll the same number:

Would you like to play in-between [y|n]? y

Die 1: 1 Die 2: 1

Number of chips: 100 Place your bet: 50

Even-steven!

Even-steven! Higher or lower [h|l]? h

Die 3: 9

* You win! *

You now have 150 chips! Play again y|n? y

Die 3: 9

* Sorry - You lose! *

You now have 100 chips! Play again [y|n]?

It shouldn't be displaying dice 3 again and saying 'you lose', it should be rolling die 1 & 2 again (basically starting from the beginning but keeping the chips that have been won/lost) Also, if the player selects 'n' it does the same thing. I want the game to end if the player selects 'n'.

Here's my code (I've changed the numbers on both dice to '1' to test the higher/lower game):

import random

# Number of chips
chipBalance = 100

play = input('Would you like to play in-between [y|n]? ')

while play == 'y':
    # First dice roll
    die1 = 1

    # Second dice roll
    die2 = 1 

    # Swaps the values of the dice if die one is larger than die two
    if die1 > die2:
        temp = die1
        die1 = die2
        die2 = temp

    # Displays value of the first and second die   
    print('\nDie 1:', die1, '  Die 2:', die2)

    # Displays the number of chips held by player
    print('\nNumber of chips:', chipBalance)

    # Prompts player to place their bet
    bet = int(input('Place your bet: '))

    #Third dice roll
    die3 = random.randint(1,12)

    # Checks if the dice are the same or different
    if die1 == die2:
        print('\nEven-steven!')
        guess = input('\nEven-steven! Higher or lower [h|l]? ')

        print('\nDie 3:', die3)

        if guess == 'h':
            if die3 > die1:
                print('\n*** You win! ***')
                chipBalance = chipBalance + bet
            elif die3 < die1:
                print('\n*** Sorry - You lose! ***')
                chipBalance = chipBalance - bet
            elif die3 == die1:
                print('\n*** You hit the post - You lose! ***')
                chipBalance = chipBalance - bet

        if guess == 'l':
            if die3 > die1:
                print('\n*** Sorry - You lose! ***')
                chipBalance = chipBalance - bet
            elif die3 < die1:
                print('\n*** You win! ***')
                chipBalance = chipBalance + bet
            elif die3 == die1:
                 print('\n*** You hit the post - You lose! ***')
                 chipBalance = chipBalance - bet

        # Displays when chip balance has reached zero
        if chipBalance <= 0:
            print('\nYou\'re all out of chips!\n\n*** GAME OVER ***')
        else:
            print('\nYou now have', chipBalance, 'chips!')

        play = input('Play again y|n? ')

    elif die1 != die2:
        print('\nNot the same, let\'s play!')

    # Value of the third die
    print('\nDie 3:', die3)

    # Results of dice roll
    if die3 > die1 and die3 < die2:
        print('\n*** You win! ***')
        chipBalance = chipBalance + bet
    elif die3 < die1 or die3 > die2:
        print('\n*** Sorry - You lose! ***')
        chipBalance = chipBalance - bet
    elif die3 == die1 or die3 == die2:
        print('\n*** You hit the post - You lose! ***')
        chipBalance = chipBalance - bet

    # Displays when chip balance has reached zero
    if chipBalance <= 0:
        print('\nYou\'re all out of chips!\n\n*** GAME OVER ***')
    else:
        print('\nYou now have', chipBalance, 'chips!')

    # Update loop control
    play = input('Play again [y|n]? ')

print('\nThanks for playing!')

Any help regarding what I need to change/what to change it to would be greatly appreciated! Thanks in advance.

¿Fue útil?

Solución 2

Your indentation is off:

   play = input('Play again y|n? ') <-- This is the expected play again

elif die1 != die2:
    print('\nNot the same, let\'s play!')

# Value of the third die  <--- This should be indented inside the elif.
print('\nDie 3:', die3)

# Results of dice roll
if die3 > die1 and die3 < die2:
    print('\n*** You win! ***')
    chipBalance = chipBalance + bet
elif die3 < die1 or die3 > die2:
    print('\n*** Sorry - You lose! ***')
    chipBalance = chipBalance - bet
elif die3 == die1 or die3 == die2:
    print('\n*** You hit the post - You lose! ***')
    chipBalance = chipBalance - bet

# Displays when chip balance has reached zero
if chipBalance <= 0:
    print('\nYou\'re all out of chips!\n\n*** GAME OVER ***')
else:
    print('\nYou now have', chipBalance, 'chips!')

# Update loop control
play = input('Play again [y|n]? ')

Otros consejos

within the block if die1 == die2 you have:

play = input('Play again y|n? ')

but at this stage it doesn't really matter what the user enters. the script will continue outside the block at

# Value of the third die
print('\nDie 3:', die3)

At the end of the even-steven section, you ask if the player wants to play again. But whether or not you go into the even-steven section, and whether or not the user asks to play again, you print Die 3 and process it. (Maybe the code after the elif die1 != die2 was supposed to be INSIDE the elif?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top