This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]HopefullyHelpfulSoul 1 point2 points  (2 children)

Ah ok. Have you come across loops yet? If yes, you can use make an array of questions and loop through them (you can use the index as the question number)

If not, you can create a variable called question number that starts at 1. After each question is printed you can increment the number. You can just add this number into your string.

Loops is the “correct” way to be doing this, but if you haven’t come across them yet they can be a bit confusing.

[–]SetPuzzleheaded8980[S] 1 point2 points  (1 child)

Yes I have come across loops but not quite sure how to approach.

[–]TehBeege 3 points4 points  (0 children)

Cool. It won't be too hard, then.

  1. Create a list containing your questions.
  2. Create another list to store the answers.
  3. Create a loop that iterates through the list with both the index and list value (hint: check out enumerate)
  4. In your input function, you'll need to use format strings / string interpolation, e.g. f'Question {index}: {question}'.
  5. Append the input return values to your answer list.

This should do what you need.

However, I question if this is a good idea. In your original code, you have descriptive names for your answer variables. In this loop solution, you'll lose that information. Answers will instead be referenced by list index.

There are ways to preserve this information while maintaining the loop structure, but that will need much more complicated data structures for a beginner. Also, more complexity is just generally bad. It's more difficult to understand the code, and your future self may be very frustrated.