Random Fact Generator

I thought it would be a fun idea to write some code that generated some random fun facts when I ran it. So I started working on it eary this morning. I was going to use a lot of stuff similar to what we did when we made the number guessing game, because it seemed the best way to make it random. However after telling my brother about the project he suggested that instead of doing it the stupid way that I was gonna do it, (like below) 

import random
randomfact =  random.randint(1,  3)
if randomfact == 1:
    print ‘There are square watermelons in Japan!’
if randomfact == 2:
    print ‘Philo Farnsworth (the man tht invented the television) had a complete undderstanding of the the theory of relativity when he was 15!’
if randomfact == 3:
    print ‘crwth is an english word with no vowel!’
if randomfact == 4:
    print ‘Horses do not yawn!’

he suggested we do it with a list. So we talked for a while about lists. Then we talked about indexs. After a lot of stupid questions from me I finally under stood why we did things like ‘random.randint (0, len(facts)-1)’. And it’s because the highest index in the list is always gonna be one less than the number of items on the list because it uses zero as a number, rather than starting t one. In the end we ended up with the finished random fact generator like the on below.

import random
facts = [
'there are square watermelons in Japan!',
'Philo Farnsworth (the man tht invented the television) had a complete undderstanding of the the theory of relativity when he was 15!',
'crwth is an english word with no vowel!',
'Horses do not yawn!',
'Elephants are the only mammal that can not jump',
'The English alphabet used to have 24 letters.',
'Matt Hummer was unanimously voted to be the coolest person ever!',
'It is more likely tht you are raped in Canada than it is for you to win the lottery',
'California has a bigger population than Canada (2009)',
'You can not hum with your nose pinched shut.'
]
indexnumber = random.randint (0, len(facts)-1)
print facts[indexnumber]

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.