Python Forum
Names for loop, Error Found !
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Names for loop, Error Found !
#1
Hey Programmers...

I try to create an names for loop (= diffrent strings in an list). But it don't work perfectly.
This is my code. I use python 3.6.2:

name1 = "Jamie"
name2 = "Stuart"
name3 = "Shelley"
name4 = "Sacha"
name5 = "Wesley"

names = list([name1, name2, name3, name4, name5])

for x in names:
    print(x)
I try to for loop the above strings in an list. But the names are printing all et once.
I get no error, but it work's imperfectly. I want to print each name per-string, called
into my list (names = list([name1... name5])). But python don't doe thad at my way.

Can anyone help my to correcting my code ?... i don't know the way about how i can
print each name per-string, called with my for loop.

Can anyone help me ?...

Thanks, Jamie.
Reply
#2
This is my output if run your code.
Output:
Jamie Stuart Shelley Sacha Wesley
Something like this is better.
Make a list then append name into that list.
>>> names = []
>>> names.append('Jamie')
>>> names.append('Stuart')
>>> names
['Jamie', 'Stuart']
>>> for name in names:
...     print(name)
...     
Jamie
Stuart
Reply
#3
[name1, name2, name3, name4, name5] is a list, then you do list([name1, name2, name3, name4, name5]). As a result names is list of list (having only one element). So you simply print one list...

Actually, I'm wrong... sorry
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  file open "file not found error" shanoger 8 1,087 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Module Not Found Error bitoded 4 1,402 Jan-01-2023, 09:08 AM
Last Post: bitoded
  File not found error saisankalpj 10 3,826 Jul-04-2022, 07:57 AM
Last Post: saisankalpj
  TypeError: sequence item 0: expected str instance, float found Error Query eddywinch82 1 5,100 Sep-04-2021, 09:16 PM
Last Post: eddywinch82
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 2 2,539 Nov-23-2020, 05:15 PM
Last Post: cananb
  Unknown error occurred: Port not found NewBeie 0 1,431 Aug-27-2020, 08:50 PM
Last Post: NewBeie
  File not found error Ads 5 3,265 Mar-15-2020, 01:56 PM
Last Post: saqib1066
  How can I sort my names of files in for loop? Mike Ru 2 2,531 Aug-02-2019, 04:56 AM
Last Post: perfringo
  Error :unable to detect undefined names created in spyder ide error at line 2 milind_eac 2 8,267 Jul-30-2019, 10:29 PM
Last Post: milind_eac
  Com Error with macro within for loop due to creating new workbook in the loop Lastwizzle 0 1,360 May-18-2019, 09:29 PM
Last Post: Lastwizzle

Forum Jump:

User Panel Messages

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