Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Your game will fall over if the user enters something for the "card number" that can't be evaluated. Also, they can enter something that isn't a suit. As @otus points out, you shouldn't use input; I would suggest you add more validation. This SO community wiki This SO community wiki will be useful to you, you should end up with something like

Your game will fall over if the user enters something for the "card number" that can't be evaluated. Also, they can enter something that isn't a suit. As @otus points out, you shouldn't use input; I would suggest you add more validation. This SO community wiki will be useful to you, you should end up with something like

Your game will fall over if the user enters something for the "card number" that can't be evaluated. Also, they can enter something that isn't a suit. As @otus points out, you shouldn't use input; I would suggest you add more validation. This SO community wiki will be useful to you, you should end up with something like

deleted 11 characters in body
Source Link
jonrsharpe
  • 14k
  • 2
  • 36
  • 62
q =if raw_input("Play again (y/n)? ").lower()
if q == "n":
 break
q = raw_input("Play again (y/n)? ").lower()
if q == "n":
 break
if raw_input("Play again (y/n)? ").lower() == "n":
 break
Source Link
jonrsharpe
  • 14k
  • 2
  • 36
  • 62

There are a couple of things I'd say you're currently missing:

  1. A way to quit;
  2. Input validation; and
  3. Scoring.

At the moment, your while True will run forever. It is not unreasonable to think that a user might eventually get bored! Therefore you could add something like:

q = raw_input("Play again (y/n)? ").lower()
if q == "n":
 break

To the end of the main loop.


Your game will fall over if the user enters something for the "card number" that can't be evaluated. Also, they can enter something that isn't a suit. As @otus points out, you shouldn't use input; I would suggest you add more validation. This SO community wiki will be useful to you, you should end up with something like

user_card = get_str_input('\nEnter a suit to pick from. Your choices are listed above. ',
 suit_choices)
...
user_number = get_int_input('\nNow pick a number card (2-9) ', 2, 9)

Where those functions only ever return valid input and catch any associated errors.


You're making a game; why not tell the player how they're doing? The simplest way would be something like:

right = wrong = 0

then += 1 to the appropriate value each time through the loop. After they quit, show something like:

print "You got {0} right and {0} wrong".format(right, wrong)
if right > wrong + 2:
 print "Well done!"
elif right > wrong - 2:
 print "Not bad"
else:
 print "Bad luck"

Also, card games lend themselves very well to OOP; you could have Card, Deck and Game classes, for example:

class Game(object):
 def __init__(self, suits=None, faces=None):
 if suits is None:
 suits = ("Diamonds", "Hearts", "Clubs", "Spades")
 if faces is None:
 faces = range(2, 10)
 self.suits, self.faces = suits, faces
 self.deck = Deck(self.suits, self.faces)
 self.deck.shuffle()
 self.score = {'right': 0, 'wrong': 0}
 def main_menu():
 ...
 def _input_suit(self):
 ...
 def _input_face(self):
 ...
 ...
lang-py

AltStyle によって変換されたページ (->オリジナル) /