【问题标题】:Python In-Between Dice Game Loops骰子游戏循环之间的 Python
【发布时间】:2014-06-24 14:52:59
【问题描述】:

我正在编写的中间骰子游戏存在问题。我之前发布了另一个问题,该问题已得到回答,但我仍然有问题。 (请记住,我对一般编程非常陌生)

这是我得到的输出,如果两个骰子掷出相同的数字,就会出现“更高或更低”选项:

你想在 [y|n] 之间玩吗?是的

模具 1:1 模具 2:1

筹码数:100 下注:50

甚至史蒂文!

甚至-史蒂文!更高还是更低 [h|l]? h

死亡 3:9

* 你赢了! *

您现在有 150 个筹码! 再玩一次y|n?是的

死亡 3:9

* 对不起 - 你输了! *

您现在有 100 个筹码! 再玩一次[y|n]?

它不应该再次显示骰子 3 并说“你输了”,它应该再次滚动骰子 1 和 2(基本上从头开始,但保留已经赢/输的筹码)另外,如果玩家选择 'n' 它做同样的事情。如果玩家选择“n”,我希望游戏结束。

这是我的代码(我已将两个骰子上的数字更改为“1”以测试更高/更低的游戏):

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!')

任何关于我需要改变什么/改变什么的帮助将不胜感激!提前致谢。

【问题讨论】:

    标签: python loops dice


    【解决方案1】:

    if die1 == die2 区块内,您拥有:

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

    但在这个阶段,用户输入什么并不重要。脚本将在

    的块外继续
    # Value of the third die
    print('\nDie 3:', die3)
    

    【讨论】:

    • 非常感谢您的回答:)
    【解决方案2】:

    您的缩进已关闭:

       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]? ')
    

    【讨论】:

    • 非常感谢,当然是这么简单:P 真的很感激!
    【解决方案3】:

    在 even-steven 部分结束时,您询问玩家是否想再玩一次。但是无论你是否进入 even-steven 部分,以及用户是否要求再次玩,你都会打印 Die 3 并处理它。 (也许elif die1 != die2 之后的代码应该在elif 内部?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-02
      • 2020-02-15
      • 2021-12-14
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多