【问题标题】:How to do multiple loops with user inputs in python terminal?如何在 python 终端中使用用户输入进行多个循环?
【发布时间】:2020-01-22 13:17:54
【问题描述】:

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

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

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.x input while-loop terminal


【解决方案1】:

如果您需要帮助,请在评论中留下明确的预期行为。我修改了一下以重用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')

【讨论】:

  • 他们是否从字符串 API 中删除了 toUpper() 和 toLower()?
  • 哦,是的,有upper()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
相关资源
最近更新 更多