2013-03-29 57 views
1

現在我遇到代碼重新啓動的問題。它重新啓動,但不會回到開始。它只是不停地問我,如果我想重新啓動。二十一點,不會重新啓動python遊戲

例如它說

The player has cards [number, number, number, number, number] with a total value of (whatever the numbers add up too.) 

--> Player is busted! 

Start over? Y/N 

Y類型和它口口聲聲說

The player has cards [number, number, number, number, number] with a total value of (whatever the numbers add up too.) 

--> Player is busted! 

Start over? Y/N 

任何人都可以請修復它,這樣它會重新啓動。 - 或告訴我如何我的代碼如下。

from random import choice as rc 
def playAgain(): 
# This function returns True if the player wants to play again, otherwise it returns False. 
print('Do you want to play again? (yes or no)') 
return input().lower().startswith('y') 
def total(hand): 
# how many aces in the hand 
aces = hand.count(11) 
t = sum(hand) 
# you have gone over 21 but there is an ace 
if t > 21 and aces > 0: 
    while aces > 0 and t > 21: 
     # this will switch the ace from 11 to 1 
     t -= 10 
     aces -= 1 
return t 
cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11] 
c2win = 0 # computer2 win 
cwin = 0 # computer win 
pwin = 0 # player win 
while True: 
player = [] 
player.append(rc(cards)) 
player.append(rc(cards)) 
pbust = False # player busted 
cbust = False # computer busted 
c2bust = False # computer2 busted 
while True: 
    tp = total(player) 
    print ("The player has cards %s with a total value of %d" % (player, tp)) 
    if tp > 21: 
     print ("--> Player is busted!") 
     pbust = True 
     print('Start over? Y/N') 
     answer = input() 
     if answer == 'n': 
      done = True 
      break 
    elif tp == 21: 
     print ("\a BLACKJACK!!!") 
     print("do you want to play again?") 
     answer = input() 
     if answer == 'y': 
      done = False 
     else: 
      break 
    else: 
     hs = input("Hit or Stand/Done (h or s): ").lower() 
     if 'h' in hs: 
      player.append(rc(cards)) 
     if 's' in hs: 
      player.append(rc(cards)) 
while True: 
    comp = [] 
    comp.append(rc(cards)) 
    comp.append(rc(cards)) 
while True: 
    comp2 = [] 
    comp.append(rc(cards)) 
    comp.append(rc(cards)) 
    while True: 
     tc = total(comp)     
     if tc < 18: 
      comp.append(rc(cards)) 
     else: 
      break 
    print ("the computer has %s for a total of %d" % (comp, tc)) 
    if tc > 21: 
     print ("--> Computer is busted!") 
     cbust = True 
     if pbust == False: 
      print ("Player wins!") 
      pwin += 1 
      print('Start over? Y/N') 
     answer = input() 
     if answer == 'y': 
      playAgain() 
     if answer == 'n': 
      done = True 
    elif tc > tp: 
     print ("Computer wins!") 
     cwin += 1 
    elif tc == tp: 
     print ("It's a draw!") 
    elif tp > tc: 
     if pbust == False: 
      print ("Player wins!") 
      pwin += 1 
     elif cbust == False: 
      print ("Computer wins!") 
      cwin += 1 
    break 
print 
print ("Wins, player = %d computer = %d" % (pwin, cwin)) 
exit = input("Press Enter (q to quit): ").lower() 
if 'q' in exit: 
    break 
print 
print 
print ("Thanks for playing blackjack with the computer!") 
+2

請修復您的縮進。 – geoffspear

+0

而不是使用多個'print'語句來創建空行(例如最後3行),只需在文本之前打印換行符:'print(「\ n \ n感謝您與計算機一起玩二十一點!」) – MattDMo

+0

有一件事你設置了一個「完成」變量,然後不做任何事情。 – geoffspear

回答

1

好玩的小遊戲,我刪除了簡潔第二的經銷商,但它應該是很容易添加回去。我改變輸入到這樣的raw_input你可以得到一個串出來,無需輸入引號。在這裏和那裏觸及了邏輯,重新格式化並添加了評論。

from random import choice as rc 

def play_again(): 
    """This function returns True if the player wants to play again, 
    otherwise it returns False.""" 
    return raw_input('Do you want to play again? (yes or no)').lower().startswith('y') 

def total(hand): 
    """totals the hand""" 
    #special ace dual value thing 
    aces = hand.count(11) 
    t = sum(hand) 
    # you have gone over 21 but there is an ace 
    while aces > 0 and t > 21: 
     # this will switch the ace from 11 to 1 
     t -= 10 
     aces -= 1 
    return t 

cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11] 
cwin = 0 # computer win 
pwin = 0 # player win 
while True: 
    # Main Game Loop (multiple hands) 
    pbust = False # player busted 
    cbust = False # computer busted 
    # player's hand 
    player = [] 
    player.append(rc(cards)) 
    player.append(rc(cards)) 
    pbust = False # player busted 
    cbust = False # computer busted 
    while True: 
     # Player Game Loop (per hand) 
     tp = total(player) 
     print ("The player has cards %s with a total value of %d" % (player, tp)) 
     if tp > 21: 
      print ("--> Player is busted!") 
      pbust = True 
      break 
     elif tp == 21: 
      print ("\a BLACKJACK!!!") 
      break 
     else: 
      hs = raw_input("Hit or Stand/Done (h or s): ").lower() 
      if hs.startswith('h'): 
       player.append(rc(cards)) 
      else: 
       break 
    #Dealers Hand 
    comp = [] 
    comp.append(rc(cards)) 
    comp.append(rc(cards)) 
    tc = total(comp) 
    while tc < 18: 
     # Dealer Hand Loop 
     comp.append(rc(cards)) 
     tc = total(comp) 
    print ("the computer has %s for a total of %d" % (comp, tc)) 
    if tc > 21: 
     print ("--> Computer is busted!") 
     cbust = True 

    # Time to figure out who won 
    if cbust or pbust: 
     if cbust and pbust: 
      print ("both busted, draw") 
     elif cbust: 
      print ("Player wins!") 
      pwin += 1 
     else: 
      print ("Computer wins!") 
      cwin += 1 
    elif tc < tp: 
     print ("Player wins!") 
     pwin += 1 
    elif tc == tp: 
     print ("It's a draw!") 
    else: 
     print ("Computer wins!") 
     cwin += 1 

    # Hand over, play again? 
    print ("\nWins, player = %d computer = %d" % (pwin, cwin)) 
    exit = raw_input("Press Enter (q to quit): ").lower() 
    if 'q' in exit: 
     break 

print ("\n\nThanks for playing blackjack with the computer!") 
+0

其說:玩家擁有總數爲20的卡片[9,11]回溯(最近的通話最後): 文件「/Users/Austin/Documents/g.py」,第45行,在 hs = raw_input() NameError:name'raw_input'未定義 >>>我該如何定義原始輸入? – user2224689

+0

如果你可以修復它,所以它的作品,我會很感激它! – user2224689

+0

啊,沒有看到你使用的是python 3x。 python3中的raw_input被重命名爲輸入。我正在運行Python 2.7。只需將所有的raw_input改回輸入。 – cmd