2017-05-08 9 views
0

コードは完璧に実行されることがあります。プレーヤーの合計が= 20かどうかを確認することは時々実行されないようです。私はこれを実行して勝者を得て再び実行することができ、20を超えて追加し続けるだけです。これは何が原因ですか?そして、関数を構築することを考える良い方法がありますか?ここでゲームは勝者を時々チェックするだけです

は私のコードです:

from random import randint 

#Setting starting space to 0, getting user names and printing 
#welcome message. 
current_space1 = 0 
current_space2 = 0 
player_1 = raw_input('Enter player 1 name: ') 
player_2 = raw_input('Enter player 2 name: ') 
print('\nWelcome ' + player_1 + ' and ' + player_2) 
print('\nLet\'s Play!\n') 


#making a function to roll dice, update current_space, check 
#to see if current_space == end of game and if not move onto 
#player_2 roll 
def roll_1(): 
    global current_space1 
    print(current_space1) 
    if current_space1 == 20: 
     print('You are the winner ' + player_1 + '!') 
    elif current_space1 != 20: 
     roll_dice = raw_input(player_1 + ' roll dice? y or n: ') 
     if roll_dice == 'y': 
      rolled_dice = (randint(1,6)) 
      print(player_1 + ' ' + 'rolled ' + str(rolled_dice)) 
      current_space1 += rolled_dice 
      print(current_space1) 
      if current_space1 == 20: 
       print('You are the winner ' + player_1 + '!') 
      elif current_space1 != 20: 
       roll_2()     


def roll_2(): 
    global current_space2 
    print(current_space2) 
    if current_space2 == 20: 
     print('You are the winner ' + player_2 + '!') 
    elif current_space2 != 20: 
     roll_dice = raw_input(player_2 + ' roll dice? y or n: ') 
     if roll_dice == 'y': 
      rolled_dice = (randint(1,6)) 
      print(player_2 + ' ' + 'rolled ' + str(rolled_dice)) 
      current_space2 += rolled_dice 
      print(current_space2) 
      if current_space2 == 20: 
       print('You are the winner ' + player_2 + '!') 
      elif current_space2 != 20: 
       roll_1() 


roll_1() 

答えて

1

current_spaceはそれが20より大きくなる可能性例があるかもしれないと20に等しい場合にのみチェックされている(たとえば、ユーザーが19のとき次のロールはif current_space1 >= 20

でうわーcurrent_space2

+0

ために同じことを行うのですか、if current_space1 == 20を交換6)

です私は気が気になりません。笑。どうもありがとうございました! – Josh

関連する問題