Computer Science Independent Project #1 – Coin Flip

Project: Coin Flip

This project will take you off-platform and get you started in your own developer environment! Never done that before? Not to worry - we’ve shared some resources to help you down below. This project can be completed entirely on your own - or, you can join the #comp-science channel in our Community Discord and find someone to work with! Jump to the community support section to hear more about this.

The project is broken down into a set of user stories for you to follow. User stories are likely something you will see more and more of as you progress in your journey as a developer. They are short, simple descriptions of a feature told from the perspective of the individual using that feature. The goal of each user story is to describe to you, the developer, what the feature or application needs to do, while providing you with enough flexibility to determine the best way to make that happen.

Overview

Objective

Build a console application that allows the user to guess the outcome of a random coin flip.

Pre-requisites

In order to complete this project, we suggest that you have familiarity with the content in the following courses or lessons from the Codecademy Computer Science Path:

  1. Milestone 1

  2. Syntax

  3. Functions

  4. Milestone 2

  5. Command line

  6. Milestone 3

  7. Control flow

  8. Loops

Suggested Technologies

Depending on where you are on your Path, there may be multiple technology options you can use to complete this project - we suggest the following:

  1. Python
  2. Command Line

Project Tasks

Get started - setting up your project

Since you won’t be completing this project in the Codecademy learning environment, you’ll need to find somewhere else to host your project! We’ve listed our recommendations below. For additional set-up information and guidance, please refer to the general resources section below.

  • Repl.it: If you’re not quite ready to get into all of the nitty gritty details of setting up your own local developer environment, Repl.it is an amazing online tool that can help you spin up a web-based development environment with the click of a button. For this project, you’ll need a Repl.it environment that supports Python 3.
  • Visual Studio Code: If you’re excited & ready to set-up your own local environment, we’ve got your back too. Visual Studio Code is a popular open source text editor that you will set-up & configure on your own device and can be used to develop websites and applications locally before pushing to production.

Basic Requirements

  • User Story: As a user I want to be able to guess the outcome of a random coin flip(heads/tails).
  • User Story: As a user I want to clearly see the result of the coin flip.
  • User Story: As a user I want to clearly see whether or not I guessed correctly.

Additional Challenges

Intermediate Challenge

  • User Story: As a user I want to clearly see the updated guess history (correct count/total count).
  • User Story: As a user I want to be able to quit the game or go again after each cycle.

Advanced Challenge

Let’s see if we can expand upon this challenge - what if instead of 2 options, there were 6?

User Story: As a user I want to be able to guess the outcome of a 6-sided dice roll (1-6), with the same feature set as the coin flip (see above).

  • You can add this directly to the existing program you’ve already written! As an additional challenge see if you can build the program such that the the user can choose between the two guessing games at startup, and possibly even switch after each cycle.

Resources & Support

Project-specific resources

  1. Coin flipping
  2. Python random module
  3. Dice
  4. Command line glossary
  5. Python documentation
  6. User Stories: What are they?

General Resources

  1. How to get set-up for coding on your computer
  2. What you need to know about Git, GitHub & Coding in Teams
  3. How developer teams work
  4. First steps in tackling a group project
  5. Resource on writing pseudocode to get started with off-platform projects
  6. Video on how to use Chrome dev tools

Community Support

Looking for additional help or someone to work with (or somewhere to brag about your finished project)? Join our Community on Discord to meet other learners like yourself!

Once you’re done…

Share on the Forums for feedback and to see some other ways of solving this problem!

21 Likes

There are several links here that do not exist.

2 Likes

Hello the link for the forum does not exist:
https://discuss.codecademy.com/c/project-feedback

I have created a simple coin flip game. please check it out and feel free to share any feedback in the comments. Thanks.

https://github.com/rishitsaraf/Computer-Science-Independent-Project-1-Coin-Flip-Python-

1 Like

Hi!

I’m new to the forums so I’m not fully sure how they work but I just completed this project on Visual Studio Code (see code below) and would love to get some feedback.

All the best :smile:

import random
on_off = True

correct_guesses = 0
incorrect_guesses = 0

while on_off:
print(“Which guessing game do you want to play? (Coin Flip or Dice Roll)”)
game = str(input())

if game == "Coin Flip":
    guess = ""
    
    while guess != "Heads" and guess != "Tails":
        print("Type your guess, Heads or Tails")
        guess = str(input())

        if guess != "Heads" and guess != "Tails":
            print("Please type either 'Heads' or 'Tails'")

    if guess == "Heads":
        print("Your Guess: Heads")
    else:
        print("Your Guess: Tails")

    answer = ""

    random_number = random.randint(1, 2)

    if random_number == 1:
        answer = "Heads"
    elif random_number == 2:
        answer = "Tails"

    print("Answer: " + answer)

    if guess == answer:
        correct_guesses += 1
        print("Your guess was corret!")
    else:
        incorrect_guesses += 1
        print("Sorry, your guess was incorrect.")

    total_guesses = correct_guesses + incorrect_guesses

    print("Total Guesses: " + str(total_guesses))
    print("Total Correct Guesses: " + str(correct_guesses))

    continue_answer = ""

    while continue_answer != "yes" and continue_answer != "no":
        print("Do you want to continue? (yes or no)")
        continue_answer = str(input())

        if continue_answer != "yes" and continue_answer != "no":
            print("Please type either 'yes' or 'no'")

    if continue_answer == "no":
        on_off = False

elif game == "Dice Roll":
    guess = ""

    while guess != "1" and guess != "2" and guess != "3" and guess != "4" and guess != "5" and guess != "6":
        print("Type your guess, 1-6")
        guess = str(input())

        if guess != "1" and guess != "2" and guess != "3" and guess != "4" and guess != "5" and guess != "6":
            print("Please type either 1, 2, 3, 4, 5 or 6")

    print("Your Guess: " + guess)

    answer = str(random.randint(1, 6))

    print("Answer: " + answer)

    if guess == answer:
        correct_guesses += 1
        print("Your guess was corret!")
    else:
        incorrect_guesses += 1
        print("Sorry, your guess was incorrect.")

    total_guesses = correct_guesses + incorrect_guesses

    print("Total Guesses: " + str(total_guesses))
    print("Total Correct Guesses: " + str(correct_guesses))

    continue_answer = ""

    while continue_answer != "yes" and continue_answer != "no":
        print("Do you want to continue? (yes or no)")
        continue_answer = str(input())

        if continue_answer != "yes" and continue_answer != "no":
            print("Please type either 'yes' or 'no'")

    if continue_answer == "no":
        on_off = False
    
else:
    print("Please type either 'Coin Flip' or 'Dice Roll'")
1 Like

way to go Felipe!

Was there a menu option at the beginning of this?

Also, did you host this somewhere or just paste the code? Thanks!

Thanks JMJ!

Not sure what you meant with “a menu option at the beginning” but there is a part at the beginning of the code which didn’t appear cohesively with the rest when I pasted it on my comment (see below).

With regards to the code, I wrote it using ‘Visual Studio Code’ and then copied it and pasted it here; not sure if there is a better way to share it on Codecademy.

Thanks again! :slight_smile: :smile:

import random
on_off = True

correct_guesses = 0
incorrect_guesses = 0

while on_off:
print(“Which guessing game do you want to play? (Coin Flip or Dice Roll)”)
game = str(input())

Hey, started my first programming course (Python 3) recently, here I built a project game. I did it in Repl.it

import random


game_chooser = input("Please choose a game: 1 - Coin Flip, 2 - Dice: ")
while True:
    
    if game_chooser == "1":
        games_count = 0
        won_count = 0
    
        print("Welcome to the Coin Flip game!")
    
        while True:
            print("Dear user, please make your guess:")
            user_guess = input("Please, enter Heads or Tails: ")
    
            side = ""
            games_count += 1
            coin_flip = random.randrange(2)
    
            if coin_flip == 1:
                side = "Heads"
            else:
                side = "Tails"
        
            print("Coin flipped at: " + side)
    
            if user_guess == side:
                print("Your guess is right!")
                won_count += 1
            else:
                print("Sorry, better luck next time.")
    
            print("Your stats: won " + str(won_count) + " games over " + str(games_count) + " total games played.")
    
            print("Play again: ")
            key = input("Yes or No? ")
            print("\n")
    
            if key == "No":
                break
    elif game_chooser == "2":
        games_count = 0
        won_count = 0
    
        print("Welcome to the Dice game!")
    
        while True:
            print("Dear user, please make your guess (1-6):")
            user_guess = int(input("Please, enter your guess: "))
    
            games_count += 1
            dice_flip = random.randrange(1, 7)
    
        
            print("Dice flipped at: " + str(dice_flip))
    
            if user_guess == dice_flip:
                print("Your guess is right!")
                won_count += 1
            else:
                print("Sorry, better luck next time.")
    
            print("Your stats: won " + str(won_count) + " games over " + str(games_count) + " total games played.")
    
            print("Play again:")
            key = input("Yes or No? ")
            print("\n")
            
            if key == "No":
                break
    
    game_switch_input = input("Do you want to switch the game? ")
    if game_switch_input == "Yes":
        game_chooser = input("Please choose a game: 1 - Coin Flip, 2 - Dice: ")
    else:
        print("Thank you for playing!")
        print("\n")
        break
    

Welcome to the fun!

Did your code perform the way you were expecting? Any insights learned along the way? What would you like feedback on?

It is, although I always trying to make the code as short as possible, and I don’t know if it’s okay, and will you learn to do it with more practice…

Looked great from what you had. I think as we go we keep learning new tricks and short cuts. No need to overcomplicate it at this point. If it works, it works. It’s always easy to look back at previous coder’s work and question motivation and tactic but for now, on this project, good enough is good enough.

Well done

1 Like

Hey guys
I’ve created the coin-flip & dice-roll game using python.
Please check it out and share feedback if any in the comments.
Thanks.

Woah! This project is actually quite fun, here is mine →

Some basic requirements included →

  • User Story: As a user I want to be able to guess the outcome of a random coin flip(heads/tails).
  • User Story: As a user I want to clearly see the result of the coin flip.
  • User Story: As a user I want to clearly see whether or not I guessed correctly.

This is my attempt at it. Some things in my code might be the long way around, but it’s simply me substituting for my lack of knowledge in python and just using whatever tools I do know how to use at the moment. I’d appreciate any feedback!

Here’s my submission I hope someone can provide feedback. I would really appreciate to receive feedback.

My notes about my project :slight_smile:

https://www.codecademy.com/workspaces/6191042be8251449412b471c

1 Like

I’m a beginner going through the Intro to CS course.
Any suggestions on how I can improve are welcome :slight_smile:

import random
coin_guess = “”
coin = random.randint(1,2)
heads_or_tails = “”
coin_guess_history = “”
coin_correct_guesses = 0
coin_incorrect_guesses = 0
coin_total_guesses = 0
play = True

dice_correct_guesses = 0
dice_incorrect_guesses = 0
dice_total_guesses = 0

while play == True:

print(“Do you want to play Coin Flip or Dice Challenge? Enter C for Coin Flip or D for Dice Challenge.”)
game = str(input())

if game == ‘C’:

  print("Type your guess, heads or tails? Lowercase please :)")
  coin_guess = str(input())
  print("")
  
  if coin_guess == "heads" or coin_guess == "tails":
    print("Your guess:", coin_guess)
  else:
    print("Try again..that's not what I asked for >=(")

  if coin == 1:
      heads_or_tails = "heads"
  else:
      heads_or_tails = "tails"

  if coin_guess == heads_or_tails:
    print("Your guess was correct! The coin landed on", heads_or_tails + "!")
    coin_correct_guesses += 1
  else:
    print("Sorry! You didn't guess correctly. The coin landed on", heads_or_tails + "!")
    coin_incorrect_guesses += 1

  coin_total_guesses = coin_correct_guesses + coin_incorrect_guesses
  print("Correct guesses:", coin_correct_guesses)
  print("Total guesses:", coin_total_guesses)

  print()

elif game == ‘D’:

  dice = random.randint(1,7)
  
  print("What do you think the die will land on? Please type a number 1 through 6 :)")
  dice_guess = str(input())
  print("")
  
  if int(dice_guess) in range(1,7):
    print("Your guess:", dice_guess)
  else:
    print("Try again..that's not what I asked for >=(")

  if dice_guess == dice:
    print("Your guess was correct! The die landed on", str(dice) + "!")
    dice_correct_guesses += 1
  else:
    print("Sorry! You didn't guess correctly. The dice landed on", str(dice) + "!")
    dice_incorrect_guesses += 1

  dice_total_guesses = dice_correct_guesses + dice_incorrect_guesses
  print("Correct guesses:", dice_correct_guesses)
  print("Total guesses:", dice_total_guesses)

  print()

print("Do you want to play again? Y/N ")

play = str(input())
if play == “N”:
play = False
elif play == “Y”:
play = True
else:
print(“Try again.”)

Here is my attempt
https://replit.com/@Cwilli2243/Coin-flip#main.py

import random
import time

# score
count = 0
correct_count = 0

# mode selection
def intro():
    print('Welcome to The Guessing Game!\n')
    mode = int(input('''
    Select a mode:
    1: Heads or tails
    2: Dice roll
    '''))
    while mode != 1 and mode != 2:
        mode = input("Please type '1' or '2'")
    return mode

chosen_mode = intro()

# core of the game
def coin_flip_guessr(mode=chosen_mode):
    
    # allows variables to be modified inside function
    global count, correct_count
    
    if mode == 1:
        current_game = "Heads or tails"
    elif mode == 2:
        current_game = "Dice roll"
    print("You're playing: " + current_game)
    
    # mode variables
    if mode == 1:
        rnum = random.randint(1,2)
        cd = 'coin'
    elif mode == 2:
        outcome = random.randint(1,6)
        cd = 'dice'
    
    if mode == 1:
        # the coin flip
        if rnum == 1:
            outcome = 'heads'
        else:
            outcome = 'tails'

        # guess taking
        guess = input('Take your guess: ')
        guess = guess.lower()
        while guess != 'heads' and guess != 'tails':
            guess = input("Please type 'heads' or 'tails': ")
    elif mode == 2:
        # guess taking
        guess = input('Take your guess: ')
        string_range = [str(num) for num in range(1,7)]
        while not any(guess == side for side in string_range):
            guess = input("Please type a number between 1 and 6: ")
        guess = int(guess)


    # the happening
    for i in range(3):
        print('.')
        time.sleep(0.5)
    print('The ' + cd + ' landed on ' + str(outcome) + '!')
    if guess == outcome:
        print('You got it right!')
        correct_count += 1
    elif guess != outcome:
        print('WRONG!!!!!!!')
    count += 1
    
    # score counter (including spelling helper)
    t_count = 'times'
    t_correct_count = 'times'
    if count == 1:
        t_count = 'time'
    if correct_count == 1:
        t_correct_count = 'time'
    print("\nYou've guessed", count, t_count, "and got it right", correct_count, t_correct_count, "\n")
    
    loop()


# quit or go again function
def loop():
    global chosen_mode
    
    p = int(input('''
    1: Try again
    2: Quit
    3: Menu
    '''))
    while not p == 1 and not p == 2 and not p == 3:
        p = int(input("Please type '1', '2', or '3'"))

    if p == 1:
        coin_flip_guessr()
    elif p == 2:
        quit()
    elif p == 3:
        coin_flip_guessr(intro())


coin_flip_guessr()

Hi, here is my solution GitHub - halb-nat/coin-flipping. I tried to code without while loops.