简体   繁体   中英

Python Dice Game/Loop error

I recently started to make a dice game in python.Everything was nice and beautiful,until now.My problem is that when I try to play and I win or lost or it's equal next time it will continue only with that.

Example:

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

and it loops like this :/

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() 

Try redefining game like this:

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()        

When you call game, you're giving new values to your random variables in each new iteration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM