Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with list
#1
hi
im trying to generat a list from some values but the list ends up kind of strange.

here how i want it to be when i print it

['test-1-1-1', 'test-1-2-2']

and here how it is

['test-1-1-1']
['test-1-1-1', 'test-1-2-2']

my code.

   def funktion(self):
        value1 = (self.a1.get())
        value2 = (self.a2.get())
        value3 = (self.a3.get())
        value4 = (self.a4.get())
        value5 = (self.a5.get())
        self.mylist = []
         
            for parts in range(value3-1, value5):
                self.mylist.append('{}-{}-{}-{}'.format(value1, value2, parts+1, parts+1))
                print (self.mylist)
What am i doing wrong?
Reply
#2
Your indentation is wrong, it should be giving you an IndentationError on line 9. Assuming that's just a mistaking in pasting the code, you need to unindent the print statement to be the same level as the for statement. As it is now it is under the for statement, so it's part of the loop and prints every time through the loop. You just want it to print once at the end of the loop, so unindent it once to take it out of the loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
yea i noticed that the indentation is wrong on line 9. Its because i modified the code a bit before i posted it and forgot about fixing the Indentation. It hade an if statement before the loop. Your right about the print line. thanks for the help.
Reply
#4
Ok no i got a new problem with the code. when im use append to continue adding to the list it over writes it instead of adding new data. i tryed extend to getting the same problem.

this is how i wanted to print

Quote:test1-1-1-1
test1-1-2-2
test1-1-3-3
test2-2-1-1
test2-2-2-2
test2-2-3-3

instead i get the last added data when i print the list like this.

Quote:test2-2-1-1
test2-2-2-2
test2-2-3-3


this is my code so far.
    def funktion(self):
        value1 = (self.a1.get())
        value2 = (self.a2.get())
        value3 = (self.a3.get())
        value4 = (self.a4.get())
        value5 = (self.a5.get())
        self.mylist = []
        
        for parts in range(value3-1, value5):
            #print('{}-{}-{}-{}\n'.format(value1, value2, parts+1, parts+1))
            #self.text_entry.insert(END, '{}-{}-{}-{}\n'.format(value1, value2, parts+1, parts+1))
            self.mylist.append('{}-{}-{}-{}'.format(value1, value2, parts+1, parts+1))
        print ('\n'.join(self.mylist))

    def file_save(self):
        print ('\n'.join(self.mylist))
Reply
#5
an example

value1 = 12
value2 = 13
value3 = 14
value4 = 15
value5 = 16
value6 = 17
mylist = []
mylist.append(value1)
mylist.append(value2)
mylist.append(value3)
mylist.append(value4)
mylist.append(value5)
mylist.append(value6)

for parts in range(2, 5):
    print(mylist[parts])
Reply
#6
Never mind i got it to work. It was a stupid mistake. The list shouldent be in the def funktion(self): every time i called def funktion it reinitialised the list.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with "Number List" problem on HackerRank Pnerd 5 2,034 Apr-12-2022, 12:25 AM
Last Post: Pnerd

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020