My Game Archive choice if/else statment malfunctions

Question:
When I run the repl and type 12 (for sudoku solver) it comes up with invalid option even though the elif statment stats that if choice == ‘12’ then play the sudoku solver.
Please help
Repl link:
https://replit.com/@creaternet101/Game-Archive

code snippet
if choice == '1':
  #function1
elif choice == '2':
  #function2
#and so on and so forth
elif choice == '12':
  os.system('clear')
  sudokusolver()
else:
  print('Invalid Option')

Hi @creaternet101 !
It appears that this issue is also around for the other choices.
Can you try doing this on line 40:

choice = str(input('''Please select a game to play:

And on line 54:

''')).lower()

And see if that works?

@NateDhaliwal
It seems that it still dosen’t work.
Any more ideas?

@creaternet101 What if you put str() around choice == '12', but remove the ' ' around 12?

I think the issue is you are attempting to convert the user input (choice) to lowercase but you have missed the brackets ()

as @NateDhaliwal mentioned before,

you have
.lower
but it should be .lower()

choice = str(input('''Please select a game to play:
1. Treasure Island
2. Number Guessing Game
3. Pong
4. Bomb Dodger
5. Password Hasher
6. Password Generator
7. Snake Game
8. Mario Game
9. Battleships
10. Ballon Popper
11. BlackJack
12. Sudoku Solver
13. Quit
''')).lower #<------put () here

once again just change .lower to .lower() (on line 54)


As you can see this gets me through the else: invalid option wall.

🤖(ai) explanation

In Python, when you use parentheses after a method or function, it indicates that you want to call or execute that function. In your example, by omitting the parentheses after .lower, you are not actually calling the lower method on the string; instead, you are referencing the method itself.

  1. If you see a method followed by parentheses (e.g., .lower()), it means you are calling that method on an object, and the method is being executed. For example, string.lower() would convert all characters in the string to lowercase.
  2. When you omit the parentheses, as in your example choice = str(input("...")).lower, you are not actually executing the lower method. Instead, you are referencing the method itself without calling it. This is similar to storing a reference to the method in a variable rather than executing it.

So, to ensure that the method is called and its result is assigned to the variable choice , you need to include the parentheses, this way, you are calling the lower method on the string returned by input , and the result (lowercased string) is assigned to the variable choice .


I’ve added print(choice) on line 55 following your choice input to demonstrate what’s happening to choice

Even though I entered 12 it becomes <built-in method lower of str object at 0x7f19c911fdf0>
This should help visualize why you are getting else:instead of elif choice == 'sudoku solver' or choice == '12':

5 Likes

I’d like to make a note here, you don’t need the str(...) wrapper on input(), as input always returns a string regardless.

2 Likes

full code snippet:

choice = input('''Please select a game to play:
1. Treasure Island
2. Number Guessing Game
3. Pong
4. Bomb Dodger
5. Password Hasher
6. Password Generator
7. Snake Game
8. Mario Game
9. Battleships
10. Ballon Popper
11. BlackJack
12. Sudoku Solver
13. Quit
''').lower().strip()
2 Likes

Well, you need to update your script with the below one.

if choice.strip() == '1':
    # function1
elif choice.strip() == '2':
    # function2
# and so on and so forth
elif choice.strip() == '12':
    os.system('clear')
    sudokusolver()
else:
    print('Invalid Option')

Thanks

Ok thanks.
coincidently, what does strip() do?

.strip() removes whitespace before and after a string.

Whitespace is characters like spaces, tabs, and newlines.

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.