-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathHigherOrLowerProcedural.py
executable file
·95 lines (69 loc) · 2.92 KB
/
HigherOrLowerProcedural.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# HigherOrLower
import random
# Card constants
SUIT_TUPLE = ('Spades', 'Hearts', 'Clubs', 'Diamonds')
RANK_TUPLE = ('Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King')
NCARDS = 8
# Pass in a deck and this function returns a random card from the deck
def getCard(deckListIn):
thisCard = deckListIn.pop() # pops one off the top of the deck and returns it
return thisCard
# Pass in a deck and this function returns a shuffled copy of the deck
def shuffle(deckListIn):
deckListOut = deckListIn.copy() # make a copy of the starting deck
random.shuffle(deckListOut)
return deckListOut
# Main code
print('Welcome to Higher or Lower.')
print('You have to choose whether the next card to be shown will be higher or lower than the current card.')
print('Getting it right adds 20 points; get it wrong and you lose 15 points.')
print('You have 50 points to start.')
print()
startingDeckList = []
for suit in SUIT_TUPLE:
for thisValue, rank in enumerate(RANK_TUPLE):
cardDict = {'rank':rank, 'suit':suit, 'value':thisValue + 1}
startingDeckList.append(cardDict)
score = 50
while True: # play multiple games
print()
gameDeckList = shuffle(startingDeckList)
currentCardDict = getCard(gameDeckList)
currentCardRank = currentCardDict['rank']
currentCardValue = currentCardDict['value']
currentCardSuit = currentCardDict['suit']
print('Starting card is:', currentCardRank + ' of ' + currentCardSuit)
print()
for cardNumber in range(0, NCARDS): # play one game of this many cards
answer = input('Will the next card be higher or lower than the ' +
currentCardRank + ' of ' +
currentCardSuit + '? (enter h or l): ')
answer = answer.casefold() # force lower case
nextCardDict = getCard(gameDeckList)
nextCardRank = nextCardDict['rank']
nextCardSuit = nextCardDict['suit']
nextCardValue = nextCardDict['value']
print('Next card is:', nextCardRank + ' of ' + nextCardSuit)
if answer == 'h':
if nextCardValue > currentCardValue:
print('You got it right, it was higher')
score = score + 20
else:
print('Sorry, it was not higher')
score = score - 15
elif answer == 'l':
if nextCardValue < currentCardValue:
score = score + 20
print('You got it right, it was lower')
else:
score = score - 15
print('Sorry, it was not lower')
print('Your score is:', score)
print()
currentCardRank = nextCardRank
currentCardValue = nextCardValue
currentCardSuit = nextCardSuit
goAgain = input('To play again, press ENTER, or "q" to quit: ')
if goAgain == 'q':
break
print('OK bye')