Python Dice Game/Need help with my script/looping!

Bob Gailer bgailer at gmail.com
Thu Nov 3 17:31:49 EDT 2016


On Nov 3, 2016 11:30 AM, "Constantin Sorin" <tolbarusorin at gmail.com> wrote:
>
> Hello,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.
> Exemple:
> Enter name >> Sorin
> Money = 2
> Bet >> 2
> You won!
> Money 4
> Bet >> 2
> You won!
> and it loops like this :/
>

What are the rules of the game?

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

When you call a function from within that function you are using recursion.
This is not what recursion is intended for. If you play long enough you
will run out of memory.

The proper way to handle this is to put the entire body of game() in a
while loop.

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

Since the values of e and f are not changed in the loop he will continue to
get the same thing.
>
> game()
> --
> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list