0

I was working on a small quiz recently and I have been trying to figure out why it inputs one answer and tries to answer all the questions with one answer I have been trying to figure this out for 2 to 3 days now, sorry if there is a simple solution I am a new coder TLDR; code takes one answer and tries to solve all questions

import random
score = 0
n = ["What is the capital of Arizona: ", "What thing in Arizona is a part of the 7 natural wonders of the world (ABRIVEATE ANSWER TO THE 1ST LETTER OF EACH WORD):  ", "what is the timezone in Arizona: "]

i = 0
answer = input(random.choice(n))
while (i < 3):
  if [0]:
    if answer == "Phoenix" or "phoenix":
      print("Correct")
      score += 1
      print(score)
    else:
        print("incorrect")

  if  [1]:
    if answer == "TGC" or "TGC":
      print("Correct")
      score += 1
      print(score)
    else:
      print("incorrect")
 
  if  [2]:
    if answer == "GMT":
      print("Correct")
      score += 1
      print(score)
    else:
      print("incorrect")
  i += 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 3
    What do you think `if [0]:` does? – Barmar Aug 25 '21 at 21:57
  • You only ask for input once, then go into a while loop, plus your if syntax is wrong. Read a good Python tutorial. – Mark Tolonen Aug 25 '21 at 21:59
  • 1
    See https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true regarding `if answer == "Phoenix" or "phoenix":` – Barmar Aug 25 '21 at 21:59
  • You're asking a random question. How is the rest of the code supposed to know which question you asked so that it can check the correct answer? – Barmar Aug 25 '21 at 22:01
  • I suggest adding `print()` statements to your code so that you can better understand what it does. To start, these can be as simple as `print("starting while loop")` or similar messages. For more suggestions about how to do this, check out [this article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Code-Apprentice Aug 25 '21 at 22:07
  • Does this answer your question? [Why does \`a == x or y or z\` always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true) – Iguananaut Aug 25 '21 at 23:23

1 Answers1

1

Compare your original to the below.

Note: I tried to change as little as possible and I'm not saying this is how to do it but to pass back your code with the minimum changed. It assumes you would like to ask all questions (I may have got that incorrect!).

import random

# track score
score = 0

# keep a list of which questions have been asked already
asked = []

# list of questions
n = ["What is the capital of Arizona: ", "What thing in Arizona is a part of the 7 natural wonders of the world (ABBREVIATE ANSWER TO THE 1ST LETTER OF EACH WORD):  ", "what is the timezone in Arizona: "]

# loop while there are unasked questions
while len(asked) < len(n):
    
    # set initial choice of 0,1,2 (same as positions in n)
    choice = random.randint(0,2)
    
    # if the choice has already been asked loop
    while choice in asked:
        choice = random.randint(0,2)

    # add the valid choice to the asked list so the question in that position doesn't get asked again
    asked.append(choice)

    # ask the question from the list n, index position 'choice'
    answer = input(n[choice])

    if choice == 0:
        if answer.lower() == "phoenix":
            print("Correct")
            score += 1
            print(score)
        else:
            print("incorrect")

    # note the 'elif' which (for reading at least) makes the code less prone to errors
    elif choice == 1:
        # note the '.lower()' so you don't need Tgc or TGc or TGC or tgC or ...you get it :o)
        if answer.lower() == 'tgc':
            print("Correct")
            score += 1
            print(score)
        else:
            print("incorrect")

    elif choice == 2:
        if answer.lower() == "gmt":
            print("Correct")
            score += 1
            print(score)
        else:
            print("incorrect")

# print the user's score starting with a couple of line returns for separation
print('\n\nYou scored {score} points for 3 questions!'.format(score=score))

Example:

What thing in Arizona is a part of the 7 natural wonders of the world (ABBREVIATE ANSWER TO THE 1ST LETTER OF EACH WORD):  TgC
Correct
1
what is the timezone in Arizona: gMt
Correct
2
What is the capital of Arizona: A
incorrect


You scored 2 points for 3 questions!
MDR
  • 2,610
  • 1
  • 8
  • 18