'How to do multiple loops with user inputs in python terminal?
I'm new to phyton and I looked everywhere, but all that I find is the .split()
option for multiple inputs but I don't want them together, I want one in each separated while loop. Here is what I have so far:
Basically I need to do 3 questions of multiple answers and 'q' is holding the answer to be tested and if right it count a point in 'pt' then the 'q' resets to be used in the next question, so I wont have 3 variables for three tiny jobs.
q =""
pt = int(0)
print('''\n\n - Answer only with A, B, C Ou D(casing doesn't matter), anything else will be disregarded. \n\n
1-) who would win, Jason Vorhees or Freddy Krugger!? \n A-) Freddy \n B-) Jason \n C-) the Public \n D-) Debatable \n\n
2-) Who is the fastest!? \n A-) Sonic \n B-) speedy \n C-) Flash \n D-) Monica \n\n
3-)Which boardgame is based on H.P.Lovecraft's stories!? \n A-) Call of Cthulhu \n B-) Sushi Go \n C-) The Resistance \n D-) Arkham Horror \n\n''')
# question 1
while(True):
q = input("answer 1: ")
if(q == "B" or q == "b"):
print("Correct!")
pt= pt+1
break
if(q == "a" or q == "c" or q == "d" or q == "A" or q == "C" or q == "D"):
print('wrong, the correct one is B \n')
break
else:
print("enter a valid letter!!!")
q=""
# question 2
while(True):
q = input("answer 2: ")
if(q == "A" or q == "a"):
print("Correct!")
pt= pt+1
break
if(q == "b" or q == "c" or q == "d" or q == "B" or q == "C" or q == "D"):
print('wrong, the correct one is A \n')
break
else:
print("enter a valid letter!!!")
q=""
# question 3
while(True):
q = input("answer 3: ")
if(q == "D" or q == "d"):
print("Correct!")
pt= pt+1
break
if(q == "a" or q == "b" or q == "c" or q == "A" or q == "B" or q == "C"):
print('wrong, the correct one is D \n')
break
else:
print("enter a valid letter!!!")
q=""
#Final
if(pt>0):
print('You got', pt, 'out of 3 questions right \n')
else:
print('unfortunately you got it all wrong.. not even to look online broh!? :v \n')
Solution 1:[1]
If you need some help, leave a comment with clear, expected behavior. I modify a bit to reuse while
sentence and if
conditions to minimize your code, here.
print('''\n\n - Answer only with A, B, C and D (casing doesn't matter), anything else will be disregarded. \n\n''')
questions = ['''1-) who would win, Jason Vorhees or Freddy Krugger!? \n A-) Freddy \n B-) Jason \n C-) the Public \n D-) Debatable \n\n''',
'''2-) Who is the fastest!? \n A-) Sonic \n B-) speedy \n C-) Flash \n D-) Monica \n\n''',
'''3-) Wich boardgame is based on H.P.Lovecraft's stories!? \n A-) Call of Cthulhu \n B-) Sushi Go \n C-) The Resistance \n D-) Arkham Horror \n\n''']
answers = ['B', 'A', 'D']
letters = ['A', 'B', 'C', 'D']
num = 0
pt = 0
while(num < 3):
print(title)
print(questions[num])
q = input("answer {}: ".format(num+1)).upper()
if q in answers[num]:
print("Correct! \n")
pt += 1
elif q in letters:
print('wrong, the correct one is {} \n'.format(answers[num]))
else:
print("enter a valid letter!!!")
num += 1
#Final
if pt > 0:
print('You got {} out of 3 questions right \n'.format(pt))
else:
print('unfortunately you got it all wrong.. not even to look online broh!? :v \n')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |