Zack
Zack

Reputation: 671

Code unexpectedly breaking

This is Python 3. In the event that a user has 0 points remaining in the pool, and has taken points from a key value and added them back into the pool, the program then breaks. This is confusing me because at this point pool > 0 and the first while loop should kick in, but it does not. Can anyone provide any explanation as to why?

PS- general code critique also appreciated. New to this.

pool = 30

attr = {'Strength' : 0, 'Health' : 0, 'Wisdom' : 0, 'Dexterity' : 0}
while pool > 0:
    print('\t\t Time to spend some points! You have', pool, 'to spend')
    spend = input('Where would you like to spend your points?')
    if spend in attr:
        amount = int(input('How many points would you like to spend?'))
        if amount > pool:
            print('You do not have that many points! You have', pool, 'points!')
        else:
            attr[spend] = amount
            pool -= amount
            print(attr)
    else:
        print('Not a valid input. Please correct.')

print('\t\t You have no more points! Add some to the pool!')

while pool == 0:
    confirm = input('Would you like to add points back into the pool?')
    if confirm == 'y':
       take =  input('Where would you like to take points from?')
       if take in attr:
            number = int(input('How many points would you like to take?'))
            if attr[take] >= number:
                pool += number
                attr[take] -= number
                print ('There are now', pool, 'points remaining') 
            elif attr[take] < number:
                    print('You dont have enough points to do that! You have', attr[take], 'points in', take)
       else:
           print('Please make a valid selection.')
    elif confirm == 'n':
        print('Goodbye!')
        break

Upvotes: 0

Views: 74

Answers (2)

rtrwalker
rtrwalker

Reputation: 1021

Once the 1st loop is finished i.e. pool==0 there is no way for the code to go back to the 1st loop.

Try this:

while pool >= 0:
    if pool>0:
        print('\t\t Time to spend some points! You have', pool, 'to spend')
        spend = input('Where would you like to spend your points?')
        if spend in attr:
            amount = int(input('How many points would you like to spend?'))
            if amount > pool:
                print('You do not have that many points! You have', pool, 'points!')
            else:
                attr[spend] = amount
                pool -= amount
                print(attr)
        else:
            print('Not a valid input. Please correct.')



    if pool == 0:
        print('\t\t You have no more points! Add some to the pool!')
        confirm = input('Would you like to add points back into the pool?')
        if confirm == 'y':
           take =  input('Where would you like to take points from?')
           if take in attr:
                number = int(input('How many points would you like to take?'))
                if attr[take] >= number:
                    pool += number
                    attr[take] -= number
                    print ('There are now', pool, 'points remaining') 
                elif attr[take] < number:
                        print('You dont have enough points to do that! You have', attr[take], 'points in', take)
           else:
               print('Please make a valid selection.')
        elif confirm == 'n':
            print('Goodbye!')
            break

Upvotes: 1

Mark Ransom
Mark Ransom

Reputation: 308130

You don't have a loop that brings you back up to the first while loop once they've added back to the pool.

The easiest fix would be to put the print and associated pool == 0 loop inside the other loop.

Upvotes: 2

Related Questions