2013-03-29 3 views
1

지금 코드를 다시 시작하는 데 문제가 있습니다. 다시 시작되지만 처음으로 되돌아 가지 않습니다. 다시 시작하고 싶은지 계속 묻습니다.Blackjack, 파이썬에서 게임을 다시 시작하지 않겠습니다.

예를 들어이

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' 문 대신에 다음과 같이 줄 바꿈 문자를 인쇄하십시오 :'print ("\ n \ n 컴퓨터로 블랙 잭을 가져 주셔서 감사합니다!)" – MattDMo

+0

글쎄, 한 가지 "완료된"변수를 설정 한 다음 아무 것도하지 않습니다. – geoffspear

답변

1

재미 작은 게임, 나는 단순에 대한 두 번째 딜러를 제거하지만, 다시 추가 할 수있을만큼 쉽게해야합니다. 난 그렇게 당신이 따옴표를 입력하지 않고 그것의 문자열을 얻을 수 raw_input을 위해 입력을 변경했습니다. 여기저기서 로직을 만졌고, 다시 formating하고 덧글을 덧붙였다.

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 : 'raw_input'이름이 정의되지 않았습니다. >>> 어떻게 원시 입력을 정의합니까? – user2224689

+0

고칠 수 있다면 고맙겠습니다. – user2224689

+0

아, 파이썬 3x를 사용하고있는 것을 보지 못했습니다. python3의 raw_input이 input으로 이름이 변경되었습니다. 파이썬 2.7을 실행 중입니다. 모든 raw_input을 다시 입력으로 변경하십시오. – cmd

관련 문제