Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loops in List
#11
(Mar-12-2017, 10:42 AM)ichabod801 Wrote:
(Mar-12-2017, 04:43 AM)Skaperen Wrote: it had been my understanding that print() would apply repr() to any non-string argument it got.

It does. The error happens before that point. First it evaluates the argument it gets, then it applies repr. The error comes in the evaluation, when + listA[i] tries to add an integer to a string.

Edit: Didn't think it through. print applies str() to any non-string argument it gets. Typing an argument into the interactive console gives the repr().

my bad for blaming it on print().  the OP was building a big string with an int in there and i didn't look at the code close enough to see that.  separate args given to print() works, too.

now you guys need to advise the OP what is the best way to code this to avoid that perl-ism mentioned earlier.

Output:
lt1/forums /home/forums 2> cat foo.py # loop in list   listA = list(range(0,101,5))   for i in range(len(listA)):     print('Item no.', i+1,  'is:', listA[i]) lt1/forums /home/forums 3> py3 foo.py Item no. 1 is: 0 Item no. 2 is: 5 Item no. 3 is: 10 Item no. 4 is: 15 Item no. 5 is: 20 Item no. 6 is: 25 Item no. 7 is: 30 Item no. 8 is: 35 Item no. 9 is: 40 Item no. 10 is: 45 Item no. 11 is: 50 Item no. 12 is: 55 Item no. 13 is: 60 Item no. 14 is: 65 Item no. 15 is: 70 Item no. 16 is: 75 Item no. 17 is: 80 Item no. 18 is: 85 Item no. 19 is: 90 Item no. 20 is: 95 Item no. 21 is: 100 lt1/forums /home/forums 4>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
Loops in List - by SandraDan - Mar-10-2017, 09:20 PM
RE: Loops in List - by wavic - Mar-10-2017, 09:30 PM
RE: Loops in List - by SandraDan - Mar-12-2017, 05:22 PM
RE: Loops in List - by Skaperen - Mar-11-2017, 08:04 AM
RE: Loops in List - by wavic - Mar-11-2017, 08:29 AM
RE: Loops in List - by Skaperen - Mar-12-2017, 04:43 AM
RE: Loops in List - by buran - Mar-11-2017, 08:50 AM
RE: Loops in List - by wavic - Mar-12-2017, 09:07 AM
RE: Loops in List - by ichabod801 - Mar-12-2017, 10:42 AM
RE: Loops in List - by Skaperen - Mar-13-2017, 03:08 AM
RE: Loops in List - by wavic - Mar-12-2017, 05:34 PM
RE: Loops in List - by wavic - Mar-13-2017, 07:42 AM
RE: Loops in List - by Skaperen - Mar-14-2017, 05:57 AM
RE: Loops in List - by ichabod801 - Mar-14-2017, 10:31 AM
RE: Loops in List - by Skaperen - Mar-15-2017, 05:13 AM
RE: Loops in List - by nilamo - Mar-20-2017, 04:07 AM
RE: Loops in List - by Skaperen - Mar-20-2017, 09:30 AM
RE: Loops in List - by snippsat - Mar-20-2017, 11:28 AM
RE: Loops in List - by Skaperen - Mar-21-2017, 02:32 AM
RE: Loops in List - by nilamo - Mar-21-2017, 02:34 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  for loops break when I call the list I'm looping through Radical 4 999 Sep-18-2023, 07:52 AM
Last Post: buran
  Using recursion instead of for loops / list comprehension Drone4four 4 3,262 Oct-10-2020, 05:53 AM
Last Post: ndc85430
  Help with List Loops slackerman73 4 2,827 Nov-25-2019, 07:49 AM
Last Post: perfringo
  Adding items in a list (using loops?) Seneca260 6 2,870 Nov-22-2019, 11:34 AM
Last Post: Seneca260
  simple list check with loops Low_Ki_ 23 15,695 Jan-09-2017, 03:58 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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