Reply
Old 05-14-2014, 10:06 AM   #1 (permalink)
All
 
Status: Member
Posts: 65



Default i need help trying to print a random word from a list in python

Right now I am trying to figure out why I cant get my print statement to produce only one value from my lists, also, how do I get it to print just the word and now ['']?

import random

adjectiveList = []
nounList =[]
sillyWordsList = []
lastNameList= []
verbList = []
illnessList = []

raw_adjectives = input("Please enter two adjectives: ")
adjectives= raw_adjectives.split(',')
adjectiveList.append(adjectives)

raw_nouns = input("Now enter two nouns: ")
nouns = raw_nouns.split(',')
nounList.append(nouns)



raw_sillyWords = input("Enter two silly words: ")
sillyWords = raw_sillyWords.split(',')
sillyWordsList.append(sillyWords)

raw_verb = input("Enter two active verbs (-ing): ")
verb = raw_verb.split(',')
verbList.append(verb)

lastName = input("Now enter a last name: ")
lastNameList.append(lastName)

illness = input("And finally, enter an illness: ")
illnessList.append(illness)

print("Your Madlib Sick Note is...")
print("Dear Professor: ", random.choice(sillyWordsList), lastNameList, """will not be attending school today. He/she has come down with a case of""", illnessList, "and has ", random.choice(adjectiveList), random.choice(nounList),".", lastNameList, """cannot stop """, random.choice(verbList),""" and we fear may never recover.

Sincerely
Mrs. """ ,random.choice(sillyWordsList))


When I run the program I get ultimately I get this format: Dear Professor: [' s', 'a']
All is offline   Reply With Quote
Old 05-14-2014, 10:06 AM   #2 (permalink)
 
Status: Senior Member
Posts: 719



Default i need help trying to print a random word from a list in python

****raw_adjectives = input("Please enter two adjectives: ")
****adjectives= raw_adjectives.split(',')
****adjectiveList.append(adjectives)

The split() method returns a list. The third line adds that list as an element of `adjectiveList`, which makes `adjectiveList` a list containing a list containing strings. There is absolutely no need for this additional list level. Remove all the *List variables, and use their non-list variants instead. So for example, to get the adjectives use

****raw_adjectives = input("Please enter two adjectives: ")
****adjectives= raw_adjectives.split(',')

and to pick one of them use `random.choice(adjectives)`.
Money07 is offline   Reply With Quote
more..
Old 05-14-2014, 10:06 AM   #3 (permalink)
 
Status: Senior Member
Posts: 207



Default i need help trying to print a random word from a list in python

what version of python are you using? if you using the latest those "input" function look fine if not use "raw_input" without the quotation marks of course
also for the print ["] part if you want to print the " mark I think it should look like this print [ " " "] you need 2 quotation marks for example print [" Anything here"] and if this doesn't help sorry for wasting your time
Henry is offline   Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can i get a random group from a a list of strings in python? Ray Programming 0 03-13-2014 11:07 PM
how to use a python list to make a random greeting? Owner101 Code Generator 2 12-28-2013 11:05 PM
PYTHON: how to print list BaniVattthami Coding 1 06-21-2011 08:07 PM
Make Python print the entire nested list AerormDof Programming 1 06-12-2011 12:37 AM
is it possible to print the items in a python list sans the lowest Gizakase Programming 1 05-23-2011 09:06 PM