Jump to content

simple issue with python

SirFriendly2525
Go to solution Solved by corrado33,

You're gunna want to use a dictionary.

 

Basically.

 

thisdict = {
  1: "Knock Knock",
  2: "A blond walks into a bar",
  3: "Insert Joke Here"
}

string randomJoke = thisdict[random.randint(1,3)]

print randomJoke

 

 

Or just put the jokes into an array and use the random number as the array index.

jokes = ["Joke1", "Joke2", "Joke3"]
string randomJoke = jokes[random.randint(0,3)]

print randomJoke

 

I am writing a super simple text based program for personal use and i am having an issue with the joke telling module. 

i want it to (when called) display a joke but at random. 

 

I am having an issue getting the random joke selection to work.

I have the following but im not sure how to get multiple jokes to be used. 

 

useJoke = random.randint(1,6)

print(useJoke)

 

Any advice or help would be great.

Thanks in Advance. 

Link to comment
Share on other sites

Link to post
Share on other sites

You're gunna want to use a dictionary.

 

Basically.

 

thisdict = {
  1: "Knock Knock",
  2: "A blond walks into a bar",
  3: "Insert Joke Here"
}

string randomJoke = thisdict[random.randint(1,3)]

print randomJoke

 

 

Or just put the jokes into an array and use the random number as the array index.

jokes = ["Joke1", "Joke2", "Joke3"]
string randomJoke = jokes[random.randint(0,3)]

print randomJoke

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, corrado33 said:

You're gunna want to use a dictionary.

 

Basically.

 


thisdict = {
  1: "Knock Knock",
  2: "A blond walks into a bar",
  3: "Insert Joke Here"
}

string randomJoke = thisdict[random.randint(1,3)]

print randomJoke

 

 

Or just put the jokes into an array and use the random number as the array index.


jokes = ["Joke1", "Joke2", "Joke3"]
string randomJoke = jokes[random.randint(0,3)]

print randomJoke

 

Thank you that helps greatly.

But do you know how would i go about including a delay in the telling of the joke?

I know i should use time.sleep(int) but i'm not sure how to include that with the dictionary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, SirFriendly2525 said:

Thank you that helps greatly.

But do you know how would i go about including a delay in the telling of the joke?

I know i should use time.sleep(int) but i'm not sure how to include that with the dictionary.

 

So you want to pause for comedic effect? Yeah that'll be a little more difficult. What you COULD do is has a different dictionary for each joke. Then it'd look something like this.

 

blondjoke1 = {
  "Opening" : "A guy walks into a bar",
  "Pause1" : 300
  "Mid Joke" : "OUCH"
  "Pause2" : 400
  "Mid Joke2" : A BLOND walks into a bar",
  "Punchline: "OUCH, OUCH, OUCH, OUCH, OUCH, OUCH, OUCH,"
}

Then just loop through the dictionary and do things based on what the "key" is (aka "opening", "pause1" etc.)

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, corrado33 said:

So you want to pause for comedic effect? Yeah that'll be a little more difficult. What you COULD do is has a different dictionary for each joke. Then it'd look something like this.

 


blondjoke1 = {
  "Opening" : "A guy walks into a bar",
  "Pause1" : 300
  "Mid Joke" : "OUCH"
  "Pause2" : 400
  "Mid Joke2" : A BLOND walks into a bar",
  "Punchline: "OUCH, OUCH, OUCH, OUCH, OUCH, OUCH, OUCH,"
}

Then just loop through the dictionary and do things based on what the "key" is (aka "opening", "pause1" etc.)

 

So i would have a dictionary for each joke and then each joke would then be placed into a larger dictionary?

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, SirFriendly2525 said:

So i would have a dictionary for each joke and then each joke would then be placed into a larger dictionary?

Hm, yeah you could definitely do that, or put them all in an array. I'm unsure if you can put dictionaries in other dictionaries, but I don't see why not. 

 

EDIT: But I'm headed to bed... good luck!

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, corrado33 said:

Hm, yeah you could definitely do that, or put them all in an array. I'm unsure if you can put dictionaries in other dictionaries, but I don't see why not. 

Awesome, thanks. I'm going to try this and ill post back to this with what happens. 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, SirFriendly2525 said:

Awesome, thanks. I'm going to try this and ill post back to this with what happens. 

Honestly, I just thought of this, but it's a better solution.

 

You should just create a "Joke" class.

 

The class would consist of the parts of the joke (perhaps in an array)

Then an array of the timings

 

Then just create a bunch of different "Jokes" using the class, and stick THOSE in an array.

 

Much... much easier. 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, corrado33 said:

Honestly, I just thought of this, but it's a better solution.

 

You should just create a "Joke" class.

 

The class would consist of the parts of the joke (perhaps in an array)

Then an array of the timings

 

Then just create a bunch of different "Jokes" using the class, and stick THOSE in an array.

 

Much... much easier. 

now i wish i was better with classes. I sort of understand them but i'm not to great with them.

I'm going to try both ideas and see what works the best. 

This should be fun... lol

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×