2014-05-07 129 views
0

我有一個問題,我正在寫一個介於兩者之間的骰子游戲。我之前發佈了另一個問題,但是我仍然遇到問題。 (請記住我是很新的編程一般)Python之間的骰子游戲循環

這是我得到的輸出,它涉及的是如果出現兩個骰子滾動相同數量的「更高或更低的」選項:

你想玩中間[y | n]嗎? Ÿ

模具1:1模具2:1個

的芯片數量:100 將您的投注:50

偶史蒂芬!

Even-steven!更高或更低[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!') 

任何幫助就什麼,我需要改變/什麼去改變它將不勝感激!提前致謝。

回答

1

你壓痕熄滅:

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

非常感謝,當然這很簡單:P真的很感激! – user3611841

1

在even-steven部分結束時,您詢問玩家是否想再玩一次。但是,無論您是否進入even-steven部分,以及用戶是否要求再次播放,都會打印Die 3並進行處理。 ?(也許elif die1 != die2後的代碼應該是裏面的elif

+0

非常感謝您的回答! – user3611841

2

塊內if die1 == die2您有:

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

,但在這個階段,用戶輸入的內容並不重要,腳本將繼續在

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

非常感謝您的回答:) – user3611841