Open Side Menu Go to the Top
Register
Python 2.7 Help a newbie Python 2.7 Help a newbie

07-19-2014 , 11:57 AM
Hey there.

I recently started to trying to learn python. I have like no prior expirience with programming other than a Commodore 64 like 35 years ago when i was a kid and typed some basic code from magazines into the commodore 64.

I understand that there are no goto commands and that writting that sort of spaggeti code is not used anymore.

Anyway i have a really hard time to understand how i can control flow my program.

Sorry before hand if its a basic or stupid question....

Im just playing around for learning so please dont mind if my code makes sense or what it could be usefull for. So far just trying to earn how to do things, then maybe later i can write something usefull.

Anyway i would like to make a small program so I can enter my openning range from various positions and see how many combo's i open from each position.

I want to make it so after I enter position variable, it will print range, and then promt if i want to try again to enter a new position or end the program.

Problem is I am not sure how can i start the position function again after i use it once. It just conteniue with the program until the program reach last line ( if that makes sense )

Here is my program so far...

def position():
position = raw_input("\n\nEnter Opening range from position:\
UTG, HJ,CO, BTN, SB: ")
if position =="UTG":
print "\n22+, A10s+, A5-A5s, K10s+, Qjs, J10s, 109s, Ajo+, KQo"
print "182 Combinations (13,73 % of hands)"

if position =="HJ":
print "\n22+, A2s+, KTs+, QTs+, JTs, T9s, 98s, 87s, ATo+, KQo"
print "222 Combinations (16,74 % of hands)"

if position =="CO":
print "\nCO Range"
print "CO % of hands)"

if position =="BTN":
print "\nBTN Range"
print "BTN % of hands)"

if position =="SB":
print "\nSB range"
print "SB % of hands)"

position()

restart = raw_input("\n\nTry again ? Y / N: ")
while restart == "Y":
position()

raw_input("\n\nPress the enter key to exit.")

####
How can i make the program so that i will be promted for the restart option each time the position function ends ?

So it will not conteniue towards the end before i enter "N" in the restart input variable ?
Python 2.7 Help a newbie Quote
07-19-2014 , 12:58 PM
forgive my sytax, its been a while since ive used python (so im not sure if its correct). The way you have it setup is an infinite loop, because restart is always going to be equal to "Y" inside the loop, because you never give the user a chance to change it. So you need to give them the ability to do so, or a way to break out

so like

Code:
position()
restart = raw_input("\n\nTry again ? Y / N: ")
while restart == "Y":
    position()
    retstart = raw_inputraw_input("\n\nTry again ? Y / N: ")
or

Code:
while True:
    position()
    restart = raw_input("\n\nTry again ? Y / N: ")
    if restart != "Y":
        break;

Last edited by Alobar; 07-19-2014 at 01:09 PM.
Python 2.7 Help a newbie Quote
07-19-2014 , 01:44 PM
Quote:
Originally Posted by Alobar
Code:
while True:
    position()
    restart = raw_input("\n\nTry again ? Y / N: ")
    if restart != "Y":
        break;
I like this approach as it seems more logical and elegant to me.
Python 2.7 Help a newbie Quote
07-19-2014 , 03:20 PM
Another possible solution:

Code:
def game_loop(c = 'y'):
        position()
        if c == 'y':
                c = raw_input('Try again: [y/n]: ')
                game_loop(c)

game_loop() ## arg is 'y' by default.

Last edited by daveT; 07-19-2014 at 03:26 PM. Reason: 3to2
Python 2.7 Help a newbie Quote
07-19-2014 , 06:24 PM
Finally got it to work, and begin to understand it after i tryed your examples.

Code:
while True:
    position()
    restart = raw_input("\n\nTry again ? Y / N: ")
    if restart != "Y":
        break;
This did it for me. Thanks a lot guys :-)

Last edited by _dave_; 07-20-2014 at 04:05 AM.
Python 2.7 Help a newbie Quote
07-20-2014 , 04:06 AM
I edited your post.

When posting code (especially python!), use [code] ... [/code], it will preserve whitespace / indentation.
Python 2.7 Help a newbie Quote
07-20-2014 , 09:36 AM
Ok thanks. Ill remember that :-)
Python 2.7 Help a newbie Quote

      
m