Re: Python Dice Game/Need help with my script/looping!
Constantin Sorin 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. > Following is a link to a version of your code rewritten to run using python3 http://csphx.net/fire_dice.py.txt The only significant differences between python2 and python3 in your code are python2 . python3 print ... print( ) raw_input( ) ... input( ) Bob Gailer wrote: > > The proper way to handle this > is to put the entire body of game() > in a while loop. > > Since the values of e and f are not changed in the loop > he will continue to get the same thing. > These changes are the key to making the program loop as desired All other changes are mostly cosmetic * Note * I am partial to white space both horizontal and vertical ... :-) -- Stanley C. Kitching Human Being Phoenix, Arizona -- https://mail.python.org/mailman/listinfo/python-list
Re: Python Dice Game/Need help with my script/looping!
On Nov 3, 2016 11:30 AM, "Constantin Sorin"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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Python Dice Game/Need help with my script/looping!
I use Linux and python 2.7.12 -- https://mail.python.org/mailman/listinfo/python-list
Python Dice Game/Need help with my script/looping!
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 :/ 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() -- https://mail.python.org/mailman/listinfo/python-list