I need help fixing up this while loop. When I place my answer I end up with the same answer “That is not A, B, C, D or E, try again:”
print("1. What is the smallest unit in life? -'2points'-")
print("A: organs")
print("B: cells")
print("C: organism")
print("D: tissues")
print("E: organ system")
print("---------------")
organs = ("A")
cells = ("B")
organisms = ("C")
tissues = ("D")
organ_system = ("E")
answers = (organs, cells, organisms, tissues, organ_system)
quest1s = input("?: ")
while quest1s != (organs, cells, organisms, tissues, organ_system):
word = input("That is not A, B, C, D or E, try again: ")
if quest1s == cells:
print("Correct, lets go on to the next question.")
elif quest1s == (organs, organisms, tissues, organ_system):
print("Incorrect, lets go on to the next question.")
The value of quest1s is a a string. But you are comparing equality against a tuple. This comparison will never be equal. Perhaps you meant:
while quest1s not in (organs, cells, organisms, tissues, organ_system):
The not in will check to see if a value is inside a container, i.e. is the string one of the values inside the tuple.
But then also you store the original user input in a variable quest1s and then use check that variable for correctness in the while loop condition. But in case the first answer is wrong, you store the next user input in a completely new variable word—when the loop condition checks quest1s again, it will not have changed.
@supercoder My reply was fairly explicit, in terms of what problems there are in the code, and what potential ways there are to fix those problems. If that’s not enough to go on, then I think that’s an indication that you would benefit from taking a step back at this point, and spending some time going through some tutorials. The Python docs themselves have an extensive one:
But there are many more online under a Google search for “Python tutorial” e.g. if a video tutorial is more your preference.
print("1. What is the smallest unit in life? -'2points'-")
print("A: organs")
print("B: cells")
print("C: organism")
print("D: tissues")
print("E: organ system")
print("---------------")
answers = ('A', 'B', 'C', 'D', 'E')
quest1s = input("?: ")
while True:
if quest1s == 'B':
print("Correct, lets go on to the next question.")
break
elif quest1s not in answers:
quest1s = input("That is not A, B, C, D or E, try again: ")
elif quest1s != 'B':
print("Incorrect, lets go on to the next question.")
break
When writing code, make sure you allow spaces between distinct parts of your code. It makes it much more readable not just for your own review, but for others as well. It takes away that cluttery look.
print("1. What is the smallest unit in life? -'2points'-")
print("A: organs")
print("B: cells")
print("C: organism")
print("D: tissues")
print("E: organ system")
print("---------------")
answers = ('A', 'B', 'C', 'D', 'E')
quest1s = input("?: ")
while quest1s not in answers:
quest1s = input("That is not A, B, C, D or E, try again: ")
if quest1s == 'B':
print("Correct, lets go on to the next question.")
else:
print("Incorrect, lets go on to the next question.")