vendredi 24 juillet 2015

Ending a while loop if a variable is less than or equal to zero in Python

For a school project, I have been making a game which allows the user to guess the end result of a football match and be able to bet on there prediction. I have been trying to break the while loop if their balance is less than or equal to zero but have been unable to do so. The whole Python code is much more extensive than what's shown below but this is the only problem.

tries = int(input("How many attempts would you like : "))

while balance > tries:
   while guesses < 2:

        print ("\nYour current balance is : £",balance)

        gameresultguess = input("\nWho will win - Home, Away, or Draw : ")
        bettingchoice = input("Would you like to bet this round - Yes or No : ")

        while True:
            if str.lower(bettingchoice) == "yes":
                bet = int(input("How much would you like to bet : "))
            if bet > balance: break
            print ("You have insuficient funds to bet this much, please try again")

        guesses +=1

        hometrue = random.randint(1,7)
        awaytrue = random.randint(1,5)

        if hometrue > awaytrue:
             gameresulttrue = "home"

        if awaytrue > hometrue:
             gameresulttrue = "away"

        if hometrue == awaytrue:
             gameresulttrue = "draw"

        if str.lower(gameresultguess) == gameresulttrue:
             print ("Correct! Nice guess! The final score was ",hometrue," : ",awaytrue)
             if str.lower(bettingchoice) == "yes":
                  balance = (balance + bet)
                  print ("Well played! Your bet has been doubled and added to your balance")
             if str.lower(bettingchoice) == "no":
                  print ("Unlucky... Should have placed a bet")

        else:
             print ("Unlucky! The final score was : ",hometrue," : ",awaytrue)
             if str.lower(bettingchoice) == "yes":
                  balance = (balance - bet)
                  print ("Oops... Better luck next time")

print ("Ouch... You went bankrupt. Try coming back when you have more money")

Aucun commentaire:

Enregistrer un commentaire