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:
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:
Python
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.
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!
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
…
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'")
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!
…
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())
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.
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!
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.”)
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()