MadDoctor5813
MadDoctor5813

Reputation: 281

Argument passing failing

I have this code:

 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)

When I run it, it appears to be stuck on the variables from easy-mode. What could be the issue? EDIT: the whole thing is here.

Upvotes: 0

Views: 55

Answers (2)

BartoszKP
BartoszKP

Reputation: 35901

The problem is here:

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

it should be:

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

or even better:

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

The statement if x == 'a' or 'b' will always be true. In Python the result of the or statement is False or the value of the first evaluated statement that wasn't False. So in your case it will be either True if difficultySelect was equal to E for example, or e. And e is always not False - that's why always the first condition was fulfilled.

Upvotes: 3

Paul H
Paul H

Reputation: 68186

Looks like your for loop is indented too far.

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)

Upvotes: 0

Related Questions