0

I am writing a battleship game to practice. When starting a game, I want to ask a question related to the difficulty of the game, and it goes as follows:

Let's start a new game. Do you want to play right away? The number of battleships will be randomized, as well as their size (y/n)

When coding the answer I want to identify the yes/no question with all writing possibilities (yes, YES, yES, Yes, etc.) but my code gets like:

easy_mode = raw_input("Let's start a new game. Do you want to play right away? The number of battleships will be randomized, as well as their size (y/n)");
if easy_mode == ("yes" or "Yes" or "YES" or "yEs" or "yeS" or "yES" or "YEs" or "YeS"):
(...)

It is indeed ugly and I was wondering whether there was an efficient method to do this.

Tim
  • 38,263
  • 17
  • 115
  • 131
  • 4
    Not only is it ugly, **it doesn't work**. `('yes' or ...)` evaluates to `'yes'`, so you're really just testing `if easy_mode == 'yes':`. – jonrsharpe Sep 22 '15 at 09:26
  • 1
    Why not a regular expression? Why English only when you can ask in every language? Too complicated - you've got bigger problems than this to worry about. – duffymo Sep 22 '15 at 09:26
  • easy_mode.upper() == "YES" – Praveen Sep 22 '15 at 09:28
  • 3
    @duffymo *"Why not a regular expression?"* - because then you have [two problems](http://regex.info/blog/2006-09-15/247)! – jonrsharpe Sep 22 '15 at 09:29
  • Your comment made me laugh @jonrsharpe. You are perfectly correct. – duffymo Sep 22 '15 at 09:30

4 Answers4

4

Since you're allowing your players to have single character answers as well, put that check in your code too:

if easy_mode.lower() in {'yes','y'}:
    (...)
hjpotter92
  • 71,576
  • 32
  • 131
  • 164
2

Try converting it to lowercase and then do checking.

easy_mode = raw_input("Let's start a new game. Do you want to play right away? The number of battleships will be randomized, as well as their size (y/n)");
if easy_mode.lower() == 'yes':
Avinash Raj
  • 160,498
  • 22
  • 182
  • 229
0

You can just convert easy_mode string to lower-case and after that you will only need to compare it with full-lowercase version of string, like this:

if easy_mode.lower() == 'yes':
    pass
GwynBleidD
  • 16,573
  • 4
  • 41
  • 66
tobyodavies
  • 23,274
  • 5
  • 37
  • 56
0

Yes, you have. You can compare after coverting to the input to lower case or uppercase first.

if easy_mode.lower() == "yes"

Or,

if easy_mode.upper() == "YES"
Ahsanul Haque
  • 9,145
  • 2
  • 27
  • 48