2016-11-03 183 views
1

我最近開始在python中做一個骰子游戲。直到現在,一切都很好,很美。我的問題是,當我嘗試玩,我贏了或輸了,或者下次它會平等只有繼續。Python骰子游戲/循環錯誤

例子:

Enter name >> Sorin 
Money = 2 
Bet >> 2 
You won! 
Money 4 
Bet >> 2 
You won! 

,並循環是這樣的:/

Here is the code: 

import time 
import os 
import random 
os = os.system 
os("clear") 

print "What is your name?" 
name = raw_input(">>") 

def lost(): 
     print "Yoy lost the game!Wanna Play again?Y/N" 
     ch = raw_input(">>") 
     if ch == "Y": 
       game() 
     elif ch == "N": 
       exit() 



def game(): 
     os("clear") 
     a = random.randint(1,6) 
     b = random.randint(1,6) 
     c = random.randint(1,6) 
     d = random.randint(1,6) 
     e = a + b 
     f = c + d 
     money = 2 
     while money > 0: 
       print "Welcome to FireDice %s!" %name 
       print "Your money: %s$" %money 
       print "How much do you bet?" 
       bet = input(">>") 
       if e > f: 
         print "you won!" 
         money = money + bet 
       elif e < f: 
         print "you lost" 
         money = money - bet 
       else: 
         print "?" 

       print money 
     lost()   

game() 
+1

你從不「重擲骰子」,可以這麼說。它們在'game()'開始時被評估,而不是循環的一部分。 – Jkdc

+0

此外,您可能還想添加一個選項以提前完成。 – kabanus

回答

0

嘗試重新定義game這樣的:

def game(): 
    os("clear") 
    money = 2 
    while money > 0: 
      print "Welcome to FireDice %s!" %name 
      print "Your money: %s$" %money 
      print "How much do you bet?" 
      bet = input(">>") 
      a = random.randint(1,6) 
      b = random.randint(1,6) 
      c = random.randint(1,6) 
      d = random.randint(1,6) 
      e = a + b 
      f = c + d 
      if e > f: 
        print "you won!" 
        money = money + bet 
      elif e < f: 
        print "you lost" 
        money = money - bet 
      else: 
        print "?" 

      print money 
    lost()   

當你打電話的遊戲,你給新值添加到每個新迭代中的隨機變量。

+0

我解僱了:)無論如何thnx幫助! –