Skip to content
Advertisement

append random choice to list and print only random choice

I am really new to python and I’m in the middle of trying to create a small ouija game.

I have a list of numbers(ages) that i have generated a random choice for I want to append that random choice to an empty list and output only the random choice number However when I run my code it outputs everything from the original list, but the if else works as it appears to output the correct text as detailed below, but lists every number in the original list

Can someone help with this?

import random

#create list of random ages
age=["12,78,14,43,54,33,23,5,85,20,54,13"]

#user input
spirit2=input("Ask me how old I am: ")

#creat empty list of what will be the random age
agenum=[]

def checkage():

    #generate random age from the list age
    agespirit=random.choice(age)
    #append the random age to the to the agenum list
    agenum.append(agespirit)

    #print to terminal
    if "12"  in agespirit:
        print("I am only",agenum,"years old")
    else:
        print("I am",agenum,"years old")

checkage()

Advertisement

Answer

You have a list that contains a single string, instead of a list of multiple strings:

age=["12,78,14,43,54,33,23,5,85,20,54,13"]

You can confirm this with print(len(age)).

This list contains a single big string.

What you want is this:

age=["12","78","14","43","54","33","23","5","85","20","54","13"]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement