while-loop help...Plz

This is the place for queries that don't fit in any of the other categories.

while-loop help...Plz

Postby Gracelogan » Sun Apr 28, 2013 9:46 am

Hey Guys....So I am a newbie to this python and programming. I am trying to get this stupid code to loop but it wont, can one of your guys have a look and let me know...

Code: Select all
import random
Play = input("Would you like to play in-between [y|n]? ")


while Play == "y":
   
    Die1 = random.randint(1,10)
    Die2 = random.randint(1,10)
    Die3 = random.randint(1,10)
   
    die1 = 0
    die2 = 0

    if Die1 > Die2:
        die1 = Die2
        die2 = Die1
        print ("Dieh 1:",die1, " " ,"Die 2:",die2," \n")
    else:
        print ("Dieg 1:",Die1, " " ,"Die 2:",Die2," \n")
       

    ChipBalance = 100


    while ChipBalance > 0:
        print("Number of chips: ", ChipBalance)
        Bet = int(input("Place your bet: "))

       
        if Die1 != Die2 or die1 != die2:
            print ("Not the same, let's play!\n")
        else:
            print ("Even-Steven\n")

       
        print("Die0 3:",Die3, " \n")

                         
        if Die3 > Die1 and Die3 < Die2 or Die3 > die1 and Die3 < die2:
            print("*** You win! ***\n")
            ChipBalance += Bet
                                 
        elif Die3 < Die1 and Die3 > Die2 or Die3 < die1 and Die3 > die2:
            print ("*** Sorry - You lose! ***\n")
            ChipBalance -= Bet

        else:
            print ("*** You hit the post - You lose double your bet! ***\n")
            ChipBalance -= (Bet*2)

           

        if ChipBalance <= 0:       
            print ("Youre all out of Chips")
            print ("Game over")

        else:
            print ("You now have",ChipBalance,"Chips\n")

            Play = input("Play again [y|n]? ")
            print ("")



OutPut
Would you like to play in-between [y|n]? y
Die 1: 8 Die 2: 9
Number of chips: 100
Place your bet: 10
Not the same, let's play!
Die 3: 3
*** Sorry - You lose! ***
You now have 90 chips!
Play again [y|n]? y
Die 1: 8 Die 2: 9
Number of chips: 90
Place your bet: 20
Not the same, let's play!
Die 3: 10
*** Sorry - You lose! ***
You now have 70 chips!
Play again [y|n]? y
Die 1: 2 Die 2: 7
Number of chips: 70
Place your bet: 20
Not the same, let's play!
Die 3: 2
*** You hit the post - You lose double your bet! ***
You now have 30 chips!
Play again [y|n]? y
Die 1: 4 Die 2: 10
Number of chips: 30
Place your bet: 10
Not the same, let's play!
Die 3: 5
18 of 21
*** You win! ***
You now have 40 chips!
Play again [y|n]? n
Thanks for playing!


Thanks for help guys
Gracelogan
 
Posts: 2
Joined: Mon Apr 22, 2013 6:28 am

Re: while-loop help...Plz

Postby Mekire » Sun Apr 28, 2013 12:24 pm

Essentially your play again input at the very end needs to be dedented twice. It should be inside the first while loop, not the second. After you run out of chips the inner one breaks; then you want to ask the question.

-Mek

Edit: Not sure if this is an assignment or just personal experimentation but the bigger issue here is the stream of consciousness coding. This should be broken up into units that each only do about one thing. I'm not sure if you have learned/been taught functions yet, but using them would help you keep track of things much easier.
User avatar
Mekire
 
Posts: 828
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: while-loop help...Plz

Postby Gracelogan » Mon Apr 29, 2013 12:42 am

its an assignment...I am doing fine but just couldnt get it loop...
Gracelogan
 
Posts: 2
Joined: Mon Apr 22, 2013 6:28 am

Re: while-loop help...Plz

Postby Kebap » Mon Apr 29, 2013 11:15 am

Quoted for future reference.. :mrgreen:
Gracelogan wrote:Hey Guys....So I am a newbie to this python and programming. I am trying to get this stupid code to loop but it wont, can one of your guys have a look and let me know...

Code: Select all
import random
Play = input("Would you like to play in-between [y|n]? ")


while Play == "y":
   
    Die1 = random.randint(1,10)
    Die2 = random.randint(1,10)
    Die3 = random.randint(1,10)
   
    die1 = 0
    die2 = 0

    if Die1 > Die2:
        die1 = Die2
        die2 = Die1
        print ("Dieh 1:",die1, " " ,"Die 2:",die2," \n")
    else:
        print ("Dieg 1:",Die1, " " ,"Die 2:",Die2," \n")
       

    ChipBalance = 100


    while ChipBalance > 0:
        print("Number of chips: ", ChipBalance)
        Bet = int(input("Place your bet: "))

       
        if Die1 != Die2 or die1 != die2:
            print ("Not the same, let's play!\n")
        else:
            print ("Even-Steven\n")

       
        print("Die0 3:",Die3, " \n")

                         
        if Die3 > Die1 and Die3 < Die2 or Die3 > die1 and Die3 < die2:
            print("*** You win! ***\n")
            ChipBalance += Bet
                                 
        elif Die3 < Die1 and Die3 > Die2 or Die3 < die1 and Die3 > die2:
            print ("*** Sorry - You lose! ***\n")
            ChipBalance -= Bet

        else:
            print ("*** You hit the post - You lose double your bet! ***\n")
            ChipBalance -= (Bet*2)

           

        if ChipBalance <= 0:       
            print ("Youre all out of Chips")
            print ("Game over")

        else:
            print ("You now have",ChipBalance,"Chips\n")

            Play = input("Play again [y|n]? ")
            print ("")



OutPut
Would you like to play in-between [y|n]? y
Die 1: 8 Die 2: 9
Number of chips: 100
Place your bet: 10
Not the same, let's play!
Die 3: 3
*** Sorry - You lose! ***
You now have 90 chips!
Play again [y|n]? y
Die 1: 8 Die 2: 9
Number of chips: 90
Place your bet: 20
Not the same, let's play!
Die 3: 10
*** Sorry - You lose! ***
You now have 70 chips!
Play again [y|n]? y
Die 1: 2 Die 2: 7
Number of chips: 70
Place your bet: 20
Not the same, let's play!
Die 3: 2
*** You hit the post - You lose double your bet! ***
You now have 30 chips!
Play again [y|n]? y
Die 1: 4 Die 2: 10
Number of chips: 30
Place your bet: 10
Not the same, let's play!
Die 3: 5
18 of 21
*** You win! ***
You now have 40 chips!
Play again [y|n]? n
Thanks for playing!


Thanks for help guys
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 282
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: cakefiend and 1 guest