""" Name: noWar.py Author: Sarah Diesburg Description: Plays the game of war with a computer player. Cards are thrown away on a tie. """ import random # These are "global variables." All functions can # see these, so be careful! (If you want to change # these variables in a function, you have to use the # global keyword (see determineOutcome function). wins = 0 losses = 0 player1Total = 0 player2Total = 0 # The rest of the code is just functions. Call main() # to run the game. def printCards(card): if card == 11: card = "J" elif card == 12: card = "Q" elif card == 13: card = "K" elif card == 1: card = "A" return card def determineOutcome(player1,player2): #List the globals we change global wins global losses global player1Total global player2Total while True: #Tie if player1 == player2: print("We tied.") #Convert to cards and print player1 = printCards(player1) player2 = printCards(player2) print("You had:", player1) print("Computer had:", player2) print() print("Better go again...") #Choose a card player1 = random.randrange(1,13) player2 = random.randrange(1,13) #Human wins elif player1 > player2: print("You won!") wins += 1 player1Total += player1+player2 #Convert to cards and print player1 = printCards(player1) player2 = printCards(player2) print("You had:", player1) print("Computer had:", player2) print() break #only loop again if there's a tie #Comptuer wins else: print("I won.") losses += 1 player2Total += player1+player2 #Convert to cards and print player1 = printCards(player1) player2 = printCards(player2) print("You had:", player1) print("Computer had:", player2) print() break #only loop again if there's a tie #end loop #output anything that might change return None def playGame(): #Choose a card player1 = random.randrange(1,13) player2 = random.randrange(1,13) #determineOutcome!!! determineOutcome(player1,player2) return None def showScore(): print() print("Your round wins:",wins) print("My round wins:",losses) print("Your card score:", player1Total) print("My card score:", player2Total) print() return None def gameExit(): print("Thanks for playing!") return None def main(): playerAnswer="" print("Welcome to the game of war.") #Keep going while we don't choose exit while playerAnswer != "e": playerAnswer = input("Press e " "to Exit, p to Play, and s to Show score ") #Input validation while playerAnswer!="e" and playerAnswer!="p" and playerAnswer != "s": print("Incorrect input.") playerAnswer = input("Press e " "to Exit, p to Play, and s to Show score ") #Show selected if playerAnswer=="s": showScore() #Play selected elif playerAnswer=="p": playGame() gameExit() return None