2016-11-03 144 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帮助! –