python


higher or lower card game in python


I need to write some code which shows a random single number/card from 1-12, the player then guesses whether the next card will be higher or lower than the first card/number. (play your cards right british game) if they guess right they get another go, if they guess wrong (they say higher or lower and it's the opposite) they lose and the game ends. if they guess four in a row right they win.
I have a very rough idea of how to go about this.
Could you please lend a hand?
import random
guessesTaken = 0
print('Hello! I am going to show you a card, guess whether the next card is higher or lower, get four in a row to win!')
number = random.randint(1, 12)
number1 = random.randint(1, 12)
number2 = random.randint(1, 12)
number3 = random.randint(1, 12)
number4 = random.randint(1, 12)
#five variables for five cards, all random cards between 1 and 12
print('Well I am thinking of a card between 1 and 12, the first number is:')
print (number) #shows them first card
while guessesTaken < 5: #limit number of guesses to make game less than 4 to win?
print('Take a guess, is the next card higher or lower? Please enter a number from 1 to 12.')
guess = input()
guess = str(guess) # limit input to "h" or "l"?
guessesTaken = guessesTaken + 1 #increment guesses taken
if guess = h and guess !> number1:
print ("you lose")
break
if guess =l and guess !< number1:
print('you lose')
if guess = h and guess !< number1:
print ("well done")
#ask about next card
if guess =l and guess !> number1:
print('well done')
#ask about next card
if guess == number1:
print ('you lose it was neither higher nor lower')
break
#basically I know the middle comparison for values higher or lower for the four cards can be done with a loop, just not sure how, very new to this.
Well, lets make your code a little more pythonic...
import random
DECK_SIZE = 5
# See that it generates 5 unique random number from 1 to 12
# (not including 13) thus simulating a deck of cards,
# where cards cannot repeat. We add a None in the end to
# indicate end of deck
deck = random.sample(range(1, 13), DECK_SIZE) + [None]
# use of the zip method to create pairs of cards to compare
# (the final card will be a None, so we can see when cards ended)
for card,next_card in zip(deck[:-1],deck[1:]):
if next_card == None: # End of deck, user won the game
print("You won")
break
# we calculate the expected correct guess
# based on the pair of cards
correct_guess = "h" if next_card > card else "l"
# We get the user input
guess = input("card is {card} is next h or l? ".format(card=card))
# We verify if user input is the expected correct guess
if guess != correct_guess:
print("You lost, card was {next_card}".format(next_card=next_card))
break
I hope you can improve your python skills by studying this code and making changes to fulfill your needs. It uses a few python idiosyncrasies that may help you get some knowledge.

Related Links

viewing github biostar-central code with graphviz
python unicode to original text character when being used as string not when printing
Pygubu Load new Window from UI File
How to use do Homographic key matching using AKAZE in opencv
Script execution time on different computers (python 3.5, miniconda)
Loss of precision while converting floats to strings in pandas
PySide application crashes when setting a new widget to QScrollArea
Python - Download File Created From ASPX Form Submission
Find number of weeks in a month
breaking up a list entry in several lists at every /n Python 3
How can I get the default colors in GTK?
Ndb entry .put() not executed?
Tensorflow: NaNs propagating throughout network, even though using sparse_softmax etc
How to do a Python argparse mutually required argument group
scaling websocket game application server
Is there a way to find a character's Unicode code point in Python 2.7?

Categories

HOME
websphere
checkbox
ocaml
boost-thread
laravel-5.3
puppet
avro
hana
search-engine
rfc
terrain
yocto
tumblr
lotus-notes
oclint
getorgchart
outlook-web-addins
google-shopping
data-synchronization
rapidjson
pyyaml
flat-file
aws-cognito
multiple-columns
uiautomator
dspic
k2
jquery-waypoints
ui5
maximo
pep8-assembly
red-black-tree
crt
helix-3d-toolkit
hotmail
geo
data-extraction
ocs
phpspreadsheet
css-counter
pox
pace
sonarqube-scan
poco-libraries
image-compression
active-model-serializers
common.logging
web-development-server
ical-dotnet
control-flow-graph
service-fabric-stateful
mod-fcgid
gauss
dwarf
tcserver
google-maps-ios
react-native-fbsdk
pycurl
search-box
uicollectionviewlayout
teiid
ingres
shinyjs
build-process
verbose
uistackview
angularjs-ng-pattern
uiprogressview
brython
gemini
gradle-release-plugin
picking
datainputstream
sem
programming-paradigms
webhdfs
team-build
ffprobe
scrollspy
jazz
stackframe
inputaccessoryview
java-melody
grunt-contrib-concat
firebug-lite
yahoo-boss-api
spdy
zipalign
anjuta
bignum
loop-invariant
sql-server-2012-web
mailcore
appfog
objcmongodb
arbor.js
code-conversion
pitch
emacs23
electronic-signature
nvelocity
hibernate3-maven-plugin
openvg
internals
inline-if
outlook-form
document-library

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App