Home > Enterprise >  Executing item of a list returns nothing
Executing item of a list returns nothing

Time:11-10

Here is my code:

def main():
    opt = setup()
    inst = ''
    if opt == 'Y' or opt == 'y':
        while not inst == 'end':
            instlist = []
            inst = input('Enter codeline to execute, when done type "end"')
            if not inst == 'end':
                instlist.append(inst)
        i = 0
        instlist
        while i < len(instlist):
            exec(instlist[i])
        i = i   1
    input()

The code works without error, but if I put in "print('hello world')" into it, it's completely blank until I press enter, and then it quits. Why isn't it printing what I told it to?

CodePudding user response:

You should put instlist = [] outside of the loop before the while.Also you should put i outside of the loop and inside the loop you should increase.Also you should use for i in range (len(inslist)) for your second loop.

CodePudding user response:

It's pretty simple. Under your while not inst == "end" loop, you have made the instlist variable. Every time the while loop is run, the list gets cleared and becomes an empty list. Try declare it just under where you have declared the inst variable.

  • Related