论点通过失败

2024-09-30 22:17:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个密码:

 def begin(): # the main attraction.
    print("Select difficulty level\n")
    print("E - Easy. Data set is 5 numbers long, and the set is sorted\n")
    print("M - Medium. Data set is 7 numbers long and the set is sorted\n")
    print("H - Hard. Data set is 10 numbers long and the set is not sorted\n")
    difficultySelect = input()
    if difficultySelect == "E" or "e":
        worksheet.beginGameLoop("easy")
    elif difficultySelect == "M" or "m":
        worksheet.beginGameLoop("med")
    elif difficultySelect == "H" or "h":
        worksheet.beginGameLoop("hard")
def beginGameLoop(gameDifficulty):
    if gameDifficulty == "easy":
        length = 5
        sorting = True
    elif gameDifficulty == "med":
        length = 7
        sorting = True
    elif gameDifficulty == "hard":
       length = 10
       sorting = False
       for questions in range(10):
           invalidPrinted = False
           questions, answer, qType = worksheet.createQuestion(length, sorting)

当我运行它时,它似乎被简单模式的变量卡住了。有什么问题吗? 编辑:整件事是here.


Tags: andthedataislengthlongprintset
2条回答

看起来你的for循环缩进太远了。你知道吗

if gameDifficulty == "easy":
    length = 5
    sorting = True
elif gameDifficulty == "med":
    length = 7
    sorting = True
elif gameDifficulty == "hard":
    length = 10
    sorting = False

for questions in range(length):
    invalidPrinted = False
    questions, answer, qType = worksheet.createQuestion(length, sorting)

问题在于:

if difficultySelect == "E" or "e":
    worksheet.beginGameLoop("easy")
elif difficultySelect == "M" or "m":
    worksheet.beginGameLoop("med")
elif difficultySelect == "H" or "h":

应该是:

if difficultySelect == "E" or difficultySelect == "e":
    worksheet.beginGameLoop("easy")
elif difficultySelect == "M" or difficultySelect == "m":
    worksheet.beginGameLoop("med")
elif difficultySelect == "H" or difficultySelect == "h":

或者更好:

if difficultySelect in ("E", "e"):
    worksheet.beginGameLoop("easy")
elif difficultySelect in ("M", "m"):
    worksheet.beginGameLoop("med")
elif difficultySelect in ("H", "h"):

语句if x == 'a' or 'b'将始终为真。在Python中,or语句的结果是False或不是False的第一个求值语句的值。所以在你的例子中,如果difficultySelect等于E,它就是True,或者e。而且e总是不是False——这就是为什么第一个条件总是被满足的原因。你知道吗

相关问题 更多 >