Python Forum
append one random intergar variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
append one random intergar variable
#1
Im super new to Python, i'm still just trying to understand the syntax
I like to add a random integer variable to a list.

Online I found different examples, for appending multiple variable to a list,
for example; the extend function or using a range,
but i just want to append one random integer variable to a list
below you find my sad attempts
Wall

from random import randint

#store num
answers = []

#Random gen
num = randint(0, 9)
print(num)
    #answers[num]
    #answers.append(num)
    #answers.insert(0, num)
    #answers = [random.randint(0,9)]
    #answers.extend(randint(0, 9))

print(answers)
Reply
#2
answers.append(num)
... should do it. Just remove the hash (#) in front of it.
Reply
#3
no not really I get a error like this:

answers.append(num)
^
IndentationError: unexpected indent
Reply
#4
Given that it says "unexpected indent", did you try removing the indentation?
Reply
#5
Yes I just did, not sure why that is a problem, but I will keep this in mind for next time. THX
Reply
#6
It's a problem because indentation has meaning in Python. It is used to define blocks of code that are executed based on the line before them.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Whys is asterisk and random variable necessary in these functions? rrowhe4d 5 1,509 Aug-05-2022, 07:53 AM
Last Post: Gribouillis
  trying to input a variable using random.choice python63 9 3,611 Aug-13-2020, 05:37 PM
Last Post: python63
  Cant Append a word in a line to a list err "str obj has no attribute append Sutsro 2 2,573 Apr-22-2020, 01:01 PM
Last Post: deanhystad
  random variable function? Novice_fanatic 4 2,772 Oct-08-2019, 01:22 PM
Last Post: jefsummers
  Guidance in Creating random output for a variable. Livne_ye 1 2,509 May-04-2019, 01:18 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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