首页 > 解决方案 > 如何在 python 终端中使用用户输入进行多个循环?

问题描述

嗨,我是 phyton 的新手,我到处找,但我发现的只是多个输入的 .split() 选项,但我不希望它们在一起,我希望在每个单独的 while 循环中都有一个.. 这是我到目前为止所拥有的

基本上我需要做3个多个答案的问题,'q'持有要测试的答案,如果正确,它会在'pt'中计算一个点,然后'q'重置以用于下一个问题,所以我不会有三个小工作的三个变量..

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-)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''')
# 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')

标签: python-3.xinputwhile-loopterminal

解决方案


如果您需要帮助,请在评论中留下明确的预期行为。我在此处进行了一些修改以重用while句子和if条件以最小化您的代码。

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')

推荐阅读