如何让用户在测验中获得2个测试答案的机会?

时间:2015-05-23 02:46:42

标签: python function

如何让用户在测验中获得2个测试答案的机会?

2 个答案:

答案 0 :(得分:5)

执行此操作的最简单方法是使用for循环,如果他们正确,则为break,如果他们从未正确执行,则可能为else。例如:

for tries in range(2):
    print("\n", "QUESTION 3:", "\n", "Which level of government is responsible for Tourism?")
    print(" a) Municipal", "\n", "b) Fedral", "\n", "c) Provincial", "\n", "d) All", "\n", "e) Legislative")
    answer3 = input("Make your choice: ")
    if answer3 == "d" or answer3 == "D" :
        print("Correct!")
        break
    else:
        print("False!")
else:
    print("Out of chances!")

如果您不想每次都重新打印问题,只需在print之前移动for来电。

链接的教程部分(以及以下几个部分)更详细地解释了这一点。

答案 1 :(得分:0)

def nTries(isCorrect, n = 2):
    answer = input("Make your choice: ")
    if isCorrect(answer):
        print("Correct")
    elif n == 1: 
        print("Out of tries, and incorrect")
    else:
        print("Incorrect")
        nTries(isCorrect, n - 1)

像这样设置

print("\n", "QUESTION 3:", "\n", "Which level of government is responsible for Tourism?")
print(" a) Municipal", "\n", "b) Fedral", "\n", "c) Provincial", "\n", "d) All", "\n", "e) Legislative")

nTries(lambda d: d.lower() == 'd')