I am making a program in python that will simulate the game of war between the u
ID: 3591406 • Letter: I
Question
I am making a program in python that will simulate the game of war between the user and the computer. I have a majority of the code working right; however, there are two things i am struggling with. I cannnot get the tie scenario to work. If the round results in a tie, the program should print that a tie occurred. It should not add the values to each players score. It should throw the cards out and immediately choose another card for each player. I was also wondering if it is possible to condense all the if statements that select the cards. I have not learned functions yet so I cannot use them. Below is my code if you could help me identify what is wrong.
import random
random.seed()
print("Welcome to the game of war.")
choice = 'P'
player_rounds_won = 0
computer_rounds_won = 0
player_score = 0
computer_score = 0
while choice != 'E' or choice !='e':
choice = input("Press e to Exit, p to Play, and s to show score ")
if choice == 'P' or choice == 'p':
player_picked = 0
computer_picked = 0
while player_picked == computer_picked:
player_picked = random.randint(1,13)
computer_picked = random.randint(1,13)
if player_picked == 1:
card_picked_player = "Ace"
if player_picked == 2:
card_picked_player = "Two"
if player_picked == 3:
card_picked_player = "Three"
if player_picked == 4:
card_picked_player = "Four"
if player_picked == 5:
card_picked_player = "Five"
if player_picked == 6:
card_picked_player = "Six"
if player_picked == 7:
card_picked_player = "Seven"
if player_picked == 8:
card_picked_player = "Eight"
if player_picked == 9:
card_picked_player = "Nine"
if player_picked == 10:
card_picked_player = "Ten"
if player_picked == 11:
card_picked_player = "Jack"
if player_picked == 12:
card_picked_player = "Queen"
if player_picked == 13:
card_picked_player = "King"
if computer_picked == 1:
card_picked_computer = "Ace"
if computer_picked == 2:
card_picked_computer = "Two"
if computer_picked == 3:
card_picked_computer = "Three"
if computer_picked == 4:
card_picked_computer = "Four"
if computer_picked == 5:
card_picked_computer = "Five"
if computer_picked == 6:
card_picked_computer = "Six"
if computer_picked == 7:
card_picked_computer = "Seven"
if computer_picked == 8:
card_picked_computer = "Eight"
if computer_picked == 9:
card_picked_computer = "Nine"
if computer_picked == 10:
card_picked_computer = "Ten"
if computer_picked == 11:
card_picked_computer = "Jack"
if computer_picked == 12:
card_picked_computer = "Queen"
if computer_picked == 13:
card_picked_computer = "King"
if player_picked == computer_picked:
player_picked = random.radint(1,13)
computer_picked = random.radint(1,13)
print("We tied.")
print("Better go again...")
print("Player had: ",card_picked_player)
print("Computer had: ",card_picked_computer)
print()
if player_picked > computer_picked:
player_rounds_won = player_rounds_won +1
player_score += player_picked + computer_picked
print("You Won!")
print("You had: ",card_picked_player)
print("Computer had: ",card_picked_computer)
print()
if computer_picked > player_picked:
computer_rounds_won = computer_rounds_won + 1
computer_score += player_picked + computer_picked
print("Computer won.")
print("You had: ",card_picked_player)
print("Computer had: ",card_picked_computer)
print()
elif choice =='S' or choice =='s':
print()
print("Player rounds won: ",player_rounds_won,)
print("Computer rounds won: ",computer_rounds_won)
print("Player card score: ",player_score)
print("Computer card score: ",computer_score)
print()
elif choice == 'E' or choice =='e':
break
else:
print("Incorrect input.")
print()
print("Thanks for playing!")
Explanation / Answer
hello my friend i have cleared your code and its working now (first code)
also i have minimized your code (second code)
please check and give thumbs up
thanks
#first code
import random
print("Welcome to the game of war.")
choice = 'P'
player_rounds_won = 0
computer_rounds_won = 0
player_score = 0
computer_score = 0
while choice != 'E' or choice !='e':
choice = input("Press e to Exit, p to Play, and s to show score ")
if choice == 'P' or choice == 'p':
player_picked = 0
computer_picked = 0
player_picked = random.randint(1,13)
computer_picked = random.randint(1,13)
if player_picked == 1:
card_picked_player = "Ace"
if player_picked == 2:
card_picked_player = "Two"
if player_picked == 3:
card_picked_player = "Three"
if player_picked == 4:
card_picked_player = "Four"
if player_picked == 5:
card_picked_player = "Five"
if player_picked == 6:
card_picked_player = "Six"
if player_picked == 7:
card_picked_player = "Seven"
if player_picked == 8:
card_picked_player = "Eight"
if player_picked == 9:
card_picked_player = "Nine"
if player_picked == 10:
card_picked_player = "Ten"
if player_picked == 11:
card_picked_player = "Jack"
if player_picked == 12:
card_picked_player = "Queen"
if player_picked == 13:
card_picked_player = "King"
if computer_picked == 1:
card_picked_computer = "Ace"
if computer_picked == 2:
card_picked_computer = "Two"
if computer_picked == 3:
card_picked_computer = "Three"
if computer_picked == 4:
card_picked_computer = "Four"
if computer_picked == 5:
card_picked_computer = "Five"
if computer_picked == 6:
card_picked_computer = "Six"
if computer_picked == 7:
card_picked_computer = "Seven"
if computer_picked == 8:
card_picked_computer = "Eight"
if computer_picked == 9:
card_picked_computer = "Nine"
if computer_picked == 10:
card_picked_computer = "Ten"
if computer_picked == 11:
card_picked_computer = "Jack"
if computer_picked == 12:
card_picked_computer = "Queen"
if computer_picked == 13:
card_picked_computer = "King"
if player_picked == computer_picked:
print("We tied.")
print("Better go again...")
print("Player had: ",card_picked_player)
print("Computer had: ",card_picked_computer)
print()
if player_picked > computer_picked:
player_rounds_won = player_rounds_won +1
player_score += player_picked + computer_picked
print("You Won!")
print("You had: ",card_picked_player)
print("Computer had: ",card_picked_computer)
print()
if computer_picked > player_picked:
computer_rounds_won = computer_rounds_won + 1
computer_score += player_picked + computer_picked
print("Computer won.")
print("You had: ",card_picked_player)
print("Computer had: ",card_picked_computer)
print()
elif choice =='S' or choice =='s':
print()
print("Player rounds won: ",player_rounds_won,)
print("Computer rounds won: ",computer_rounds_won)
print("Player card score: ",player_score)
print("Computer card score: ",computer_score)
print()
elif choice == 'E' or choice =='e':
break
else:
print("Incorrect input.")
print()
print("Thanks for playing!")
#second code
import random
print("Welcome to the game of war.")
choice = 'P'
player_rounds_won = 0
computer_rounds_won = 0
player_score = 0
computer_score = 0
aa = ['Ace','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King']
while choice != 'E' or choice !='e':
choice = input("Press e to Exit, p to Play, and s to show score ")
if choice == 'P' or choice == 'p':
player_picked = 0
computer_picked = 0
player_picked = random.randint(1,13)
computer_picked = random.randint(1,13)
card_picked_player = aa[player_picked-1]
card_picked_computer = aa[computer_picked-1]
if player_picked == computer_picked:
print("We tied.")
print("Better go again...")
print("Player had: ",card_picked_player)
print("Computer had: ",card_picked_computer)
print()
if player_picked > computer_picked:
player_rounds_won = player_rounds_won +1
player_score += player_picked + computer_picked
print("You Won!")
print("You had: ",card_picked_player)
print("Computer had: ",card_picked_computer)
print()
if computer_picked > player_picked:
computer_rounds_won = computer_rounds_won + 1
computer_score += player_picked + computer_picked
print("Computer won.")
print("You had: ",card_picked_player)
print("Computer had: ",card_picked_computer)
print()
elif choice =='S' or choice =='s':
print()
print("Player rounds won: ",player_rounds_won,)
print("Computer rounds won: ",computer_rounds_won)
print("Player card score: ",player_score)
print("Computer card score: ",computer_score)
print()
elif choice == 'E' or choice =='e':
break
else:
print("Incorrect input.")
print()
print("Thanks for playing!")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.