Coin Flip Game Project - Intermediate

Hi All this is my very first programming project since I joined codecademy last year (lol).
I have done up to intermediate challenge. Below is my code.
Have a nice day!

import random

retry_decision = "Y"
correct_count = 0
total_count = 0

def flip_machine():
    rand = random.randint(0,1)
    if rand == 0:
        return "head"
        return "tail"

def comparer(result):
    if result == guess:
        return "Correct"
        return "Incorrect"

# User Story 5: As a user I want to be able to quit the game or go again after each cycle.

def retry_decide():
    retry_decision = ""
    count_2 = 0 
    while count_2 == 0:
        if retry_decision != "Y" and retry_decision != "N":
            print("Do you want to retry?")
            retry_decision = input("Y/N:")
        elif retry_decision == "N":
            count_2 += 1
    return retry_decision

# User Story 2: As a user I want to clearly see the result of the coin flip.
# User Story 3: As a user I want to clearly see whether or not I guessed correctly.

def result_announcer():
    result = flip_machine()
    print("Your guess is " + guess)
    print("The result is... " + result)
    print("So your guess is..."+ comparer(result) + "!")
    print("Your guess successful rate is:" + str(rate_counter(comparer(result))) + "%")

# User Story 4: As a user I want to clearly see the updated guess history (correct count/total count).

def rate_counter(comparer_result):
    global correct_count, total_count
    if comparer_result == "Correct":
        correct_count += 1
    total_count += 1
    rate = correct_count / total_count * 100
    return rate

while retry_decision == "Y":
# User Story 1: As a user I want to be able to guess the outcome of a random coin flip(heads/tails).
    print("Guess whether a coin will be head or tail?")
    guess = input("(head/tail):")
    count = 0
    while count == 0:
        if guess != "head" and guess != "tail":
            print("Choose head or tail!")
            guess = input("(head/tail):")
            count += 1



Hello! Did you have a question, or did you just want feedback?


Hello! Could you give me some feedback? … Or just some discussion as I want to share my work with others who may be doing the same project.
Some reflection:

  1. Structure
    Maybe I should put the while loop for user story 1 in a function?
  2. Retry mechanism
    Maybe there is smarter way to do it?
  3. Readability
    Is it easy to understand?


Hello! The structure seems good to me, however I’m not entirely sure what the program is attempting to do. One way to do the retry mechanism would be to do something like this:

user_choice = input("Y or N") #sees if the user wants to play
while user_choice == "Y": #as long as the user says Y, the game will play
  play_game() #the is the game function. It could be any name
  user_choice = input("Y or N") #this sees if the user wants to play again.
#if they do, and enter Y, then the loop will repeat. Else, the loop will exit
print("THE GAME HAS ENDED")#just something to let the user know the game's ended

this is a bit simple, however the idea is the same.

1 Like

Many thanks for your suggestion!

1 Like