Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

What is the best way to write this program?

I am writing a python program that will have several lists/databases. Each database will have the answers to one of five questions: Who, what, when, where, and why.

In the who database, there might be things like "the president of the united states" or "your high school english teacher". In the what, it would be what they're doing, so "is jumping on the bed" or "is running a marathon". When is "2000 years ago" or "in the '50s". Where is (obviously) their location, and why is their purpose.

The code would number each option in a certain list, and then would use a random number generator to select one bit from each list and show them to you.

Any ideas of the best way to structure this code? I have no idea. :) Thanks!

1 Answer

I would probably put it into a do while loop. As the user for something like. Press any key to display a question. This would be the "main" part of the program. I would start it like this.

class a a = true do As the user for input. if input is 1 display a question call class b and make it do the work

if input is 2 exit out of program. a = false while(a)

NEW CLASS

class b

You said databases but I will assume you have several lists.

5 lists. Who, what, when, where, and why.

I need a method to generate a random number.

[[[[Now you can do this several different ways but I would put the random number in a loop. To run 5 times.]]]]]]

random number 1 = 3. Get list 1 index 3 value and save it. random number 2 = 5 get list 2 index 5 value and save it. etc etc 5 times.

then I need to return a string to class a, that it can display. String question = value1 + value2 \etcetc return question

[[[ you can also return more than one value but for the sake of simplicity I would do it like this]]]

Run Down You have a loop that ask the user if they want to display a question or quite. User says yes, you generate 5 random numbers. Each number is applied to a different list representing the index on that list. By using the list index, you obtain a value and save it. Then you create a string out of the 5 saved values, and you push it back to the main thread to displayy the question.

What you do after that is up to you. If you need to answer the question. Just put some if statments in to determine what to do with the user input provided. Don't forget to consider if you ask for 1 and the user types 'one' to handle those types of errors.

That is a brief description of how I would start.

Hope it helps.

Thanks!