Tuesday, October 29, 2013

A Quick Tip - The Randint of a List

     You'll notice that in our hot dog program, it said this:

print(condimentlist[randomCondiments])

     In this article, we will be focusing on that. So, what the heck did we do here? Well, for starters, the only kind of random function I know so far is randint, so I had to get a little complicated. But, this is the randint of a list. We might be able to see this better if we have a simpler program that focuses mainly on that. Here's a program I wrote a long time ago for an online site, but then I realized it doesn't run Python.

import random
question=0
questionsAsked=0
fortune=0
fortune2=37
theOutput=["Yes.", "Call me AskIt!", "No.", "Maybe.", "Yes. Now go die in a hole.", "Impossible!", "Those who say impossible accomplish nothing.", "Listen to your heart and you will know the right answer.", "No way.", "Stay healthy, pet a squirrel, and wash your hair with blue cheese.", "You need to take a break off of the computer.", "If I had a body, I would strangle you.", "NU WAI!!! TEL MEE MOAR!!!!!!111!!", "NU NORBZ AWR LIEK TOTEZEZ EPIK LIEK UR BEEG TOEZ!!!!1", "I am not sure how to respond to that.", "...", "Without a doubt.", "[ Content Deleted ] JK! Try asking again.", "Just... No.", "Erik Cassel made it.", "Try asking again, and use proper grammar.", "i nu undirstandz ur grammarz.", "I can't hear you! Speak up!", "Ummm... No.", "Hey, you should do that!", "Certainly.", "It is uncertain.", "As Babe Ruth always said, 'Go home and die.'", "It is very unlikely.", "It is very likely.", "NO WAY!!", "Gross!", "Please be appropriate", "No, hackers suck.", "Errrrrrrrrrrrrrr...", "Have a fun time!", "You should not do it", "He who lacks confidence never achieves his goal."]

print("Hello, there!")
while questionsAsked < 9001:
    question=input()
    fortune=random.randint(0, fortune2)
    print(theOutput[fortune])
    theOutput.append(question)
    fortune2=fortune2 + 1
    fortune=random.randint(0, fortune2

     I know, that are some weird lines. I just wrote those to be funny. Unfortunately, I couldn't figure out how to let the user ask unlimited questions, so I made the limit 9,000. Anyway, the variables we'll be focusing on are fortune, fortune2, and theOutput.
     I guess this wasn't the best example, because I made it repeat whatever you type, but let's just go with it. So, fortune's value is 0. And fortune2's value is 37. And theOutput is a big, long list, consisting of around 36 strings. So, in Python, from 0 to the amount of items in the list +1, It is 0 to 37. Either that, or I just made a mistake and fortune2 is supposed to be 36, but I haven't seen a problem with the program yet. So, you know that when you want to print a specific item in a list, you do print(VAR[NUM])? Well, since fortune is a randint function, it counts as an integer, which qualifies as the NUM in the previous statement. So, when you run the program, you get a random item from theOutput each time! And that explains our hot dog program. Anyway, now you can have fun with AskIt!
     I realized something. Do I really need to have while questionsAsked < 9001:? I challenge you to take that line out and see how the program runs!

1 comment: