Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help for list printing
#1
Hi
Except if i am mistaken, the function below create an empty list and append random number in this list

def toto():
    jp = []
    for i in range(4):
        jp.append(random.randint(1,10))
I try to display all the list number like this
print(*jp) but it doesn't works
Same thing if i try to print the first item
print(jp[0])
what is wrong please?
Reply
#2
How do you try this? Your function does not return anything. I think that is the problem. Your function must return a value and then you must use this return value.
import random

def toto():
    jp = []
    for i in range(4):
        jp.append(random.randint(1,10))
    return jp

a = toto()
print(a)
print(a[0])
Output:
[1, 3, 3, 10] 1
Reply
#3
If you are trying to print jp outside of the function (def), it won't work because jp has gone out of scope.

if you return the value, you will be able to print the values
Example:
>>> import random
>>> def toto():
...     jp = []
...     for i in range(4):
...         jp.append(random.randint(1,10))
...     return(jp)
... 
>>> jp = toto()
>>> print(*jp)
3 1 5 7
>>> print(jp[0])
3
>>>
Reply
#4
both I and ibreeden posted at the same time.
his return is the proper way.
Reply
#5
There is choices for this purpose in random module. So actually there is no need for repeated calls of randint.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
(Apr-29-2021, 10:43 AM)ibreeden Wrote: How do you try this? Your function does not return anything. I think that is the problem. Your function must return a value and then you must use this return value.
import random

def toto():
    jp = []
    for i in range(4):
        jp.append(random.randint(1,10))
    return jp

a = toto()
print(a)
print(a[0])
Output:
[1, 3, 3, 10] 1

many thanks
Reply
#7
(Apr-29-2021, 10:44 AM)Larz60+ Wrote: If you are trying to print jp outside of the function (def), it won't work because jp has gone out of scope.

if you return the value, you will be able to print the values
Example:
>>> import random
>>> def toto():
...     jp = []
...     for i in range(4):
...         jp.append(random.randint(1,10))
...     return(jp)
... 
>>> jp = toto()
>>> print(*jp)
3 1 5 7
>>> print(jp[0])
3
>>>

many thanks
Reply
#8
So actually there is no need for repeated calls of randint.
Reply
#9
@jp1 Try to remember: LEGB How Python Finds Things
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Printing through list.. again jesse68 2 1,142 Apr-16-2022, 03:24 PM
Last Post: jesse68
  Problem printing last element from a list tester_V 3 2,404 Oct-30-2020, 04:54 AM
Last Post: tester_V
  Printing empty list? hhydration 2 2,123 Oct-28-2020, 11:34 AM
Last Post: Atekka
  Printing images from a list Heyjoe 4 2,829 Jun-22-2020, 02:28 AM
Last Post: Heyjoe
  printing a list contents without brackets in a print statement paracelx 1 2,128 Feb-15-2020, 02:15 AM
Last Post: Larz60+
  Printing List in one line bharat_s579 6 4,131 May-26-2019, 08:30 PM
Last Post: perfringo
  list is printing incorrectly.. anna 1 2,090 May-18-2019, 12:12 PM
Last Post: ichabod801
  Printing one thing from a list bidoofis 1 2,340 Feb-05-2019, 09:02 PM
Last Post: ichabod801
  Printing a new python list abdallah786 3 85,883 Aug-17-2018, 03:21 PM
Last Post: buran
  [Help] List of Lists not printing both Cmder/Anaconda? vanicci 4 3,560 Aug-15-2018, 03:00 PM
Last Post: vanicci

Forum Jump:

User Panel Messages

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