0

Hello awesome coding folks!

Does anyone have a good idea to add a random number to a list? I am trying to get a list to log the random numbers that are generated inside a loop. Here is an example of the code inside the loop:

stuff = {'name': 'Jack', 'age': 30, 'height': '6 foot 9 inches'}

tester = [0]

print(tester)

tester.append[random.randint(1, len(stuff))]

print(tester)

Apparently the output of random.randint is not subscriptable, but I'm not sure how else to write this.

Thank you in advance for the help!

1
  • My guess: You want to generate 4 random numbers and add it to the tester list? Commented Apr 6, 2015 at 3:00

3 Answers 3

3
tester.append[random.randint(1, len(stuff))]
#      wrong ^                       wrong ^

# should be
tester.append(random.randint(1, len(stuff)))

Methods, such as append, are called with parentheses rather than brackets.

Sign up to request clarification or add additional context in comments.

Comments

0

It's simple, Try this

from random import randint # import randint from random
listone = [] # Creating a list called listone
for i in xrange(1,10): # creating a loop so numbers can add one by one upto 10 times
   ic = randint(1,10) # generating random numbers from 1 to 10
   listone.append(ic) # append that numbers to listone
   pass
print(listone) # printing list
# for fun you can sort this out ;)
print(sorted(listone))

Comments

0

do this modifications in your code

import random
stuff = {'name': 'Jack', 'age': 30, 'height': '6 foot 9 inches'}

tester = [0]

print(tester)

tester.append(random.randint(1, len(stuff)))

print(tester)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.