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')
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.
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.
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 .
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':
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()
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')