簡體   English   中英

Python骰子游戲/循環錯誤

[英]Python Dice Game/Loop error

我最近開始在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() 

嘗試重新定義這樣的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()        

當您調用游戲時,您將在每次新迭代中為隨機變量賦予新值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM