#United States of America


War Card Game

This is a version of the card game War, and it is also my final Python A project.

# War Card game # A player wins if they gain 52 cards or have more cards after 5 rounds. These amounts can be adjusted if needed. # There are 2 checks placed in the code for player and opponent deck contents, these show the movement of cards. # A tie breaker within a tie breaker is decided with a coin flip in this version of the game. from random import randint total_cards = ["2", "2", "2", "2", "3", "3", "3", "3", "4", "4", "4", "4", "5", "5", "5", "5", "6", "6", "6", "6", "7", "7", "7", "7", "8", "8", "8", "8", "9", "9", "9", "9", "10", "10", "10", "10", "11", "11", "11", "11", "12", "12", "12", "12", "13", "13", "13", "13", "14", "14", "14", "14"] player_cards = [] opponent_cards = [] for x in range(0, 26): y = 51-x card_number = randint(0, y) card = total_cards[card_number] player_cards.append(card) total_cards.pop(card_number) for x in range(0, 26): y = 25-x card_number = randint(0, y) card = total_cards[card_number] opponent_cards.append(card) total_cards.pop(card_number) print("player") print(player_cards) print("opponent") print(opponent_cards) # This is a check to see the starting moves, feel free to remove if using the code. p = len(player_cards) o = len(opponent_cards) moves = 0 while p != 52 and o != 52 and moves < 5: p_card = int(player_cards[0]) o_card = int(opponent_cards[0]) print("You have placed down a ", p_card) print("The opponent has placed down a ", o_card) moves = moves + 1 if p_card > o_card: continue_playing = input("Your card is higher, take all cards and keep playing? Enter yes to continue: ") if continue_playing == "yes": player_cards.append(str(o_card)) player_cards.pop(0) player_cards.append(str(p_card)) opponent_cards.pop(0) elif o_card > p_card: continue_playing = input("Your card is lower, give opponent cards and keep playing? Enter yes to continue: ") if continue_playing == "yes": opponent_cards.append(str(p_card)) opponent_cards.pop(0) opponent_cards.append(str(o_card)) player_cards.pop(0) elif o_card == p_card: continue_playing = input("The cards are the same, time for a tie-breaker? Enter yes to continue: ") if continue_playing == "yes": print("You and the opponent place 3 cards face down and one face up. Lets see who wins the war!") p_card1 = player_cards[1] p_card2 = player_cards[2] p_card3 = player_cards[3] p_card4 = int(player_cards[4]) o_card1 = opponent_cards[1] o_card2 = opponent_cards[2] o_card3 = opponent_cards[3] o_card4 = int(opponent_cards[4]) print("You place down a ", p_card4, "and your opponent has placed down a ", o_card4) if p_card4 > o_card4: print("Congrats! You won the war and gain a ", o_card1, o_card2, o_card3, "and", o_card4) player_cards.pop(0) player_cards.pop(1) player_cards.pop(2) player_cards.pop(3) player_cards.pop(4) player_cards.append(str(p_card)) player_cards.append(str(p_card1)) player_cards.append(str(p_card2)) player_cards.append(str(p_card3)) player_cards.append(str(p_card4)) opponent_cards.pop(0) opponent_cards.pop(1) opponent_cards.pop(2) opponent_cards.pop(3) opponent_cards.pop(4) player_cards.append(str(o_card)) player_cards.append(str(o_card1)) player_cards.append(str(o_card2)) player_cards.append(str(o_card3)) player_cards.append(str(o_card4)) elif o_card4 > p_card4: print("You lost the war, your opponent gains a ", p_card1, p_card2, p_card3, "and", p_card4) opponent_cards.pop(0) opponent_cards.pop(1) opponent_cards.pop(2) opponent_cards.pop(3) opponent_cards.pop(4) opponent_cards.append(str(o_card)) opponent_cards.append(str(o_card1)) opponent_cards.append(str(o_card2)) opponent_cards.append(str(o_card3)) opponent_cards.append(str(o_card4)) player_cards.pop(0) player_cards.pop(1) player_cards.pop(2) player_cards.pop(3) player_cards.pop(4) opponent_cards.append(str(p_card)) opponent_cards.append(str(p_card1)) opponent_cards.append(str(p_card2)) opponent_cards.append(str(p_card3)) opponent_cards.append(str(p_card4)) elif o_card4 == p_card4: print("Wow, another tie! You placed a ", p_card4, "and your opponent placed a ", o_card4, ".") p_coin = input("Lets decide this one with a coin flip. Heads or tails? ") continue_playing = input("Would you like to keep playing? Enter yes to continue: ") coin = randint(1, 2) if coin == 1 and p_coin == "heads" and continue_playing == "yes": print("You won the coin flip and gain the cards!") player_cards.pop(0) player_cards.pop(1) player_cards.pop(2) player_cards.pop(3) player_cards.pop(4) player_cards.append(str(p_card)) player_cards.append(str(p_card1)) player_cards.append(str(p_card2)) player_cards.append(str(p_card3)) player_cards.append(str(p_card4)) opponent_cards.pop(0) opponent_cards.pop(1) opponent_cards.pop(2) opponent_cards.pop(3) opponent_cards.pop(4) player_cards.append(str(o_card)) player_cards.append(str(o_card1)) player_cards.append(str(o_card2)) player_cards.append(str(o_card3)) player_cards.append(str(o_card4)) elif coin == 2 and p_coin == "tails": print("You won the coin flip and gain the cards!") player_cards.pop(0) player_cards.pop(1) player_cards.pop(2) player_cards.pop(3) player_cards.pop(4) player_cards.append(str(p_card)) player_cards.append(str(p_card1)) player_cards.append(str(p_card2)) player_cards.append(str(p_card3)) player_cards.append(str(p_card4)) opponent_cards.pop(0) opponent_cards.pop(1) opponent_cards.pop(2) opponent_cards.pop(3) opponent_cards.pop(4) player_cards.append(str(o_card)) player_cards.append(str(o_card1)) player_cards.append(str(o_card2)) player_cards.append(str(o_card3)) player_cards.append(str(o_card4)) else: print("You lost the coin flip and your opponent gains the cards.") opponent_cards.pop(0) opponent_cards.pop(1) opponent_cards.pop(2) opponent_cards.pop(3) opponent_cards.pop(4) opponent_cards.append(str(o_card)) opponent_cards.append(str(o_card1)) opponent_cards.append(str(o_card2)) opponent_cards.append(str(o_card3)) opponent_cards.append(str(o_card4)) player_cards.pop(0) player_cards.pop(1) player_cards.pop(2) player_cards.pop(3) player_cards.pop(4) opponent_cards.append(str(p_card)) opponent_cards.append(str(p_card1)) opponent_cards.append(str(p_card2)) opponent_cards.append(str(p_card3)) opponent_cards.append(str(p_card4)) # This is another check to see how the content of the decks has changed during the round. print(player_cards) print(opponent_cards) p = len(player_cards) o = len(opponent_cards) if p > o: print("You have won the game!") elif o > p: print("You lost, the opponent won this game.") else: print("You and the opponent have tied.")

Rock Paper Scissors

This is a typical game of rock paper scissors coded using python.

# Rock Paper Scissors from random import randint loop = input("Do you want to play rock paper scissors? Enter yes or no: ") while loop != "no": number = randint(0, 2) plays = ["rock", "paper", "scissors"] opponent = plays[number] play = str(input("Type in either rock, paper or scissors: ")) if (play != "rock") and (play != "paper") and (play != "scissors"): print("You may have misspelled a word") elif play == opponent: print("Tie! ") elif opponent == "rock": if play == "scissors": print("You lose, rock beats scissors. ") elif play == "paper": print("You win, paper beats rock. ") elif opponent == "paper": if play == "rock": print("You lose, paper beats rock. ") elif play == "scissors": print("You win, scissors beats paper") elif opponent == "scissors": if play == "rock": print("You win, rock beats scissors") elif play == "paper": print("You lose, scissors beat paper") loop = input("Do you want to play again? Enter either yes or no: ") print("Thanks for playing!")

Hangman Game

This is a typical hangman game that is coded using python and uses turtle for the graphical aspects.

# Hangman from random import randint import turtle s = turtle.getscreen() t = turtle.Turtle() t.shapesize(1, 1, 1) t.penup() t.goto(-250, -220) t.pendown() t.goto(-190, -220) t.penup() t.goto(-220, -220) t.pendown() t.goto(-220, 220) t.goto(0, 220) t.goto(0, 170) number = randint(0, 19) words = ["tiger", "television", "cinnamon", "racetrack", "lemonade", "closet", "leopard", "smoothie", "strawberry", "snowman", "pineapple", "counter", "coyote", "sewing", "movement", "paradox", "computer", "eighteen", "musical", "fantastic"] word = words[number] length = len(word) wrong_guesses = [] x = "_" underscore_list = [] for i in range(length): underscore_list.append(x) while len(wrong_guesses) < 6 and "_" in underscore_list: letter_guess = input("Input a letter you think is in the word: ") indexes = [index for index, element in enumerate(word) if element == letter_guess] if letter_guess in word: print("Nice job, that is one of the letters in the word!") for i in range(0, len(indexes)): place = indexes[0] indexes.remove(indexes[0]) underscore_list[place] = letter_guess else: wrong_guesses.append(letter_guess) print("That letter is not in the word, keep trying :)") guesses_left = 6 - len(wrong_guesses) if len(wrong_guesses) == 1: t.penup() t.goto(0, 110) t.pendown() t.circle(30) elif len(wrong_guesses) == 2: t.goto(0, -20) elif len(wrong_guesses) == 3: t.penup() t.goto(0, 90) t.pendown() t.goto(-50, 70) elif len(wrong_guesses) == 4: t.penup() t.goto(0, 90) t.pendown() t.goto(50, 70) elif len(wrong_guesses) == 5: t.penup() t.goto(0, -20) t.pendown() t.goto(-55, -70) print("You have", guesses_left, "wrong guesses left") print("These are the correct letters in the word you have guessed so far: ", underscore_list) print("These are the letters that are not in the word:", wrong_guesses) if "_" not in underscore_list: print("Nice job, you guessed the word and won the game!") elif len(wrong_guesses) == 6: t.penup() t.goto(0, -20) t.pendown() t.goto(55, -70) t.penup() t.goto(-15, 145) t.pendown() t.goto(-10, 140) t.penup() t.goto(-10, 145) t.pendown() t.goto(-15, 140) t.penup() t.goto(15, 145) t.pendown() t.goto(10, 140) t.penup() t.goto(10, 145) t.pendown() t.goto(15, 140) print("You have no more wrong guesses left, this game is lost :(") turtle.done()

2 Number Calculator

This code will allow you to use many operations with 2 numbers, not just the 4 basic operations!

print("Please insert two numbers and what you want to do with them") a = float(input("Please enter Number a here")) b = float(input("Please enter Number b here")) Operation = input("Please enter operation here. Please use the words Addition, Subtraction, Division, Multiplication, " "Square Root of a, Square Root of b, a choose b, b choose a,\n a to the power of b, and b to the" "power of a. If you don't want 2 numbers, please enter a random number in that slot. Thank you.") if Operation == "Addition": print(a+b) elif Operation == "Subtraction": print(a-b) elif Operation == "Division": print(a/b) elif Operation == "Multiplication": print(a*b) elif Operation == "a choose b": c = math.comb(int(a), int(b)) print(c) elif Operation == "b choose a": d = math.comb(int(b), int(a)) print(d) elif Operation == "a to the power of b": g = pow(int(a), int(b)) print(g) elif Operation == "b to the power of a": h = math.comb(int(b), int(a)) print(h) elif Operation == "Square root of a": j = math.sqrt(int(a)) print(j) elif Operation == "Square root of b": k = math.sqrt(int(b)) print(k) else: print("The operation given is not valid. Please try again.")

Regular Polygon and Circle Area Calculator

This will allow you to find the approximate area of several regular polygons and circles.

import math a = float(input("Please enter what regular polygon you want to find the area of. Options area a circle, equilateral " "triangle, square, pentagon, hexagon, octagon")) b = float(input("Please enter side length or radius of your polygon")) if a == "circle": print(3.1415 * b * b) elif a == "triangle": print(b * b * 0.433) elif a == "square": print(b * b) elif a == "pentagon": print(1.72 * b * b) elif a == "hexagon": print(2.598 * b * b) elif a == "octagon": print(4.828 * b * b) else: print("Sorry, either your polygon or length/radius is wrong")

Triangle Checker

This code will allow you to find if your triangle is equilateral, isosceles, scalene, acute, obtuse, or/and right!

Angle_A = float(input("Please enter value for angle 1 of a triangle")) Angle_B = float(input("Please enter value for angle 2 of b triangle")) Angle_C = float(input("Please enter value for angle 3 of c triangle")) if Angle_A + Angle_B + Angle_C != 180: print("Sorry, the angles you entered are not valid, please try again.") elif Angle_A > 90 or Angle_B > 90 or Angle_C > 90: print("Your triangle is Obtuse") elif Angle_A == 90 or Angle_B == 90 or Angle_C == 90: print("Your triangle is a Right triangle") elif Angle_A < 90 and Angle_B < 90 and Angle_C < 90: print("Your triangle is acute") elif Angle_A == Angle_B or Angle_B == Angle_C or Angle_A == Angle_C: print("Your triangle is isosceles") elif Angle_A != Angle_B != Angle_C: print("Your triangle is scalene") elif Angle_A == Angle_B == Angle_C: print("Your triangle is equilateral")
1