Python骰子游戏/循环错误

时间:2016-11-03 16:32:00

标签: python

我最近开始在python中制作一个骰子游戏。到目前为止,一切都很美好。我的问题是,当我尝试游戏时,我赢了或输了,或者下次相等时它只会继续。< / p>

示例:

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 个答案:

答案 0 :(得分: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()        

当您调用游戏时,您会在每次新迭代中为随机变量赋予新值。