开发者

My Python items aren't adding to the list, len isn't changing after recalling

开发者 https://www.devze.com 2023-03-14 21:45 出处:网络
Alright, I\'m making a game of sorts... I\'ve done it with append as well -_- When I call back and add the next item(i.e. #2 after doing #1) I get just that item in the print out...

Alright, I'm making a game of sorts... I've done it with append as well -_-

When I call back and add the next item(i.e. #2 after doing #1) I get just that item in the print out...

such as this:

>  1
You put your Hatchet into the bag
Press enter to add more items
['Hatchet']
>  
>  2
You put your Toothbrush into the bag
Press enter to add more items
['Toothbrush']

I'd like for the bag to get more items in it and once the bag reaches three items it takes me to the next 'level'. I can't seem to add the items to the bag and keep them in the bag and then monitor the growing len(bag). If I call back the len(bag) it's always stuck back on 1. Is this because of the 'return' function I'm using? or is it something else? I figure I can try coding so that it goes into a new list with new bag after entering one item(seems like a ton of excessive code, but would work). I'm also pretty confident there is tons of excessive code in this script, I'm extremely new to Python and am doing an assignment/exercise from a book. I appreciate the help in advance!!

def beginning():
    print 'Hello'

def func():
    firstitem=raw_input(">  ")
    bag=[]
    limit=len(bag)
    a="Hatchet"
    b="Toothbrush"
    c="Map"

    if firstitem=="1":
        bag.insert(1, 'Hatchet')
        print 'You put your %s into the bag' % a
        print 'Press enter to add more items'
        print bag
        limit=len(bag)
        if limit == 开发者_StackOverflow中文版int(3):
            beginning()
        item=raw_input(">  ")
        return func()
    if firstitem=="2":
        bag.insert(2, 'Toothbrush')
        print 'You put your %s into the bag' % b
        print 'Press enter to add more items'
        print bag
        limit=len(bag)
        if limit == int(3):
            beginning()
        item=raw_input(">  ")
        return func()
    if firstitem=="3":
        bag.insert(3, 'Map')
        print 'You put your %s into the bag' % c
        print 'Press enter to add more items'
        print bag
        limit=len(bag)

    if limit == int(3):
            beginning()
        item=raw_input(">  ")
        return func()

OK so if I make the new func. is that under the existing one? or complete new?


Your problem is that you're adding something to bag, but then you're recursing into the start of func again, where it has a different scope. It then assigns [] to bag, leaving it empty. Something you could do is make func take an optional argument:

def func(bag=None):
    if bag is None:
        bag = []
    # ...
    return func(bag)

Additionally, append probably really is the right thing to do here.


bag is a list not a dict. If you want to add items to a list, you should use append.


The reason why you always have the only one item in bag list is because you recreate it each time you call your function so you need to find a way how to correctly initialize it. Also it is better to use append instead of insert in your case. Please find below my sample code(not sure that this is a good idea to write code this way - so don't beat me too much for it):

def beginning():
    print 'Hello'
    return []

def func(bag):
    item = raw_input("Enter your choice here >  ")
    limit = len(bag)
    things = {'1':"Hatchet", '2':"Toothbrush", '3':"Map", '77':'Leave', '100':'backward'}
    if things.get(item).lower() == 'leave':
        print 'Bye'
        return
    elif things.get(item).lower() == 'backward':
        while len(bag)!=0:
            print bag, bag.pop()
        return
    bag.append(things.get(item))
    print 'You put your %s into the bag' % things.get(item)
    print 'Press enter to add more items'
    print bag
    if len(bag) == 3:
        bag = beginning()
    return func(bag)

func(beginning())
0

精彩评论

暂无评论...
验证码 换一张
取 消