数字修正

时间:2018-01-18 19:24:23

标签: python python-3.x

我正在用令我困惑的部分编写代码。

while answer1 != 'a':
    if answer1 == 'b':
        print('\nWrong answer.\n')
    answer1= input("\nEnter again.\nYou only have one more try!\n")
    amount = amount+1
    print(amount)#for testing
    if amount == 1:
        print("\nTry next question")
    break

如果用户说“b'两次。我已将金额设为1,但如果用户说a,那么它将是2。我想要做的是尝试两个如果' b'有两次,但有一次,如果' a'曾经说过一次。

1 个答案:

答案 0 :(得分:0)

所以,这里的总体目标尚不清楚。但是,我认为这可能会帮助你:

首先,放弃while answer != 'a',然后转到while True,依靠循环内的条件来处理break / continue

amount = 1 # Initiate amount outside of the loop, otherwise it'll get reset on each loop.
while True: # Run until base condition is met.
    print("I am the question")
    ANSWER = input("Answer: ")
    if ANSWER == 'a' or amount >= 2: # Base condition.
        print(amount)
        break 
    else: # This runs if answer isn't what you want.
        print("Wrong, please try again.")
        amount += 1
        continue

最佳