Program won't loop or add to a list

58 views Asked by At

I have made (or more, I'm trying to make) a program to help me calculate some figures. I have some levels that each give a reward and to give out the rewards I prefer to not insert each amount and add them all up to get the totals.

I've made it so that I write the level number, it adds the amount to a list, it loops again, I insert a different number etc.

But it doesn't loop or add the numbers to the list.

Heres my super un-compact code:

lists = []
total = 0
def moneyz():
    level=input('-> ')
    print('g') #just a testing bookmark
    print(level) #same here
    if level==1:
        print('oo') #and here
        lists.apped('150')
        total==total+150

    elif level == 2:
        lists.apped('225')
        total==total+225
        moneyz()

    elif level == 3:
        lists.apped('330')
        total==total+330
        moneyz()

    elif level == 4:
        lists.apped('500')
        total==total+500
        moneyz()

    elif level == 5:
        lists.apped('1000')
        total==total+1000
        moneyz()

    elif level == 6:
        lists.apped('1500')
        total==total+1500
        moneyz()

    elif level == 7:
        lists.apped('2250')
        total==total+2250
        moneyz()

    elif level == 8:
        lists.apped('3400')
        total==total+3400
        moneyz()

    elif level == 9:
        lists.apped('5000')
        total==total+5000
        moneyz()

    elif level == 10:
        lists.apped('15000')
        total==total+15000
        moneyz()


moneyz()
print(lists)
print(total)
2

There are 2 answers

1
Samwise On BEST ANSWER

I can see three bugs in this code:

  1. level is a str, so it will never equal an int. None of your if checks will ever be satisfied, which is why your function isn't recursing. A way to spot this in debugging would have been to add a print(repr(level)) after you receive the input; you'd see that it's a value like '1' (a string) instead of 1 (an integer).
  2. There is no such thing as apped(), so once you hit that line of code (which currently isn't happening because your if checks never match), it'd raise an AttributeError.
  3. Your total is never going to increase because you're using the == (equality check) operator rather than the = (assignment) operator.

Here's a much shorter (working) version of the program, using a simple lookup table in place of a bunch of if statements:

# Rewards for levels 0 to 10.
rewards = [0, 150, 225, 330, 500, 1000, 1500, 2250, 3400, 5000, 15000]

# Running totals.
lists = []
total = 0

while True:
    # Get reward level from the user.  If not a valid reward level, stop.
    level = input('-> ')
    try:
        level_num = int(level)
    except ValueError:
        break
    if level_num not in range(len(rewards)):
        break

    # Add the reward to the lists and the total.
    reward = rewards[level_num]
    lists.append(reward)
    total += reward

# Final output.
print(lists)
print(total)
1
Umesh Jadhav On

You are using level==1where level is a string as input() returns a string and you are comparing it with int.

You should try level=='1' or convert level to int by level = int(input("->")).

Also, list has append() method and not apped()

Also, total==total+1000 won't help for adding. It will just check if value of total is equal to total plus 1000. You should use total = total + 1000 for adding value.

Here is one sample modified if block:

if level=='1':
        print('oo') #and here
        lists.append('150')
        total=total+150

Hope it helps.