Coin flip project

Hello everyone,

I have completed my first small project using python3. It is the coin flip project. During this project we were also provided hints on what functions to use but I did not look at any of the hints. I did however have to sometimes search up how to use a certain prompt (such as random.choice) other than that I planned everything I needed to do in my head. I had to complete the basic requirements however I went up to additional requirements as-well. I also have a solution for the advanced requirements in my head but I didn’t write them down. As I was doing this project my brain did not want to use any functions (such as def function():). However, the next day I tried to integrate more use of def function(): and I was successful until I got down to the Intermediate Challenge my brain again wanted to get rid of def function(): so, I did and I have attached my code to this prompt. I know my code is not as flexible as I can be and I know everyone thinks differently so could you please give me some feedback on how I did and if my code is good enough while considering what I was required to do?

Here is my screen shot of my code:

If the screenshot does not work here is my code:

import random
num = 0;
g = 0;
while (g != 2):
        
        print("Guess heads by entering 1 or tails by entering 2 for this coin flip."); #asking first question
        answer = input()                                                               #waiting for users response
        if(answer == 1):                                                               #printing heads if user chose heads
            print("You guessed heads.")
        else:                                                                          #printing tails if user chose tails
            print("you guessed tails.")
        flip = random.choice([1,2])                                                    #flips the coin
        if(flip == 1):                                                                 #determines the outcome when flip is equal to 1 or something else and prints heads or tails 
            print("The coin landed on Heads.")
        else:
            print("The coin landed on Tails.")
        if(answer == flip):                                                            #prompts user whether their guess was right or wrong
            print("Congrats you guessed right.")
        else:
            print("Sorry your guess was wrong.")
        num += 1
        
        print("You have guessed " + str(num) + " times.")

        print("Would you would like to guess again enter 1 or enter 2 to exit?")
        g = input()
        if(g == 2):                                                                   #printing heads if user chose heads
            break
        else:
            continue

Here are the Requirements:
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.

Hey there @system4441718981 :grinning:

Well there are a few errors in your code.
Currently one can not play, as the computer will always assume they entered the number two. The reason for this is in your method of collecting input. input() always returns a string and:

>> 1 == "1"
False

Since there is only one if statement and this evaluates to False, it is running your else.

Thankfully you can convert a string to an integer with int(). I would say start with that to improve your code.


One other thing I see is that their is no way to enter an invalid input. Even if I type ‘a’ it will still run through the code. Perhaps add a way to tell the player if their input is invalid.

when ever I run the code it works for me

In my code I have g initally set to 0 and im using the != expression in my while loop. So the computer should assume that when g is not the number 2 it will run the code. I dont see why its not working for you.

In the code I have g initally set to 0 and im using the != expression in my while loop. So the computer should assume that when g is not the number 2 it will run the code. I dont see why its not working for you.

Try entering the number 1, the code is running for me so that it assumes you are entering 2.


I don’t see what point you’re trying to make?

are you referring to g? if so I don’t see why I wouldn’t set g to equal 0 to begin the loop.

Is the code your running exactly the same as what you posted earlier?
If I run the code you posted earlier, here your code will not run correctly.

It sees all input as being a string and you if gates always return False.

I am reffering to the way you use input() to assign values to you variables.

Since input() always return a string then it ends up like so:

g = 0
g = input()

>>2

if (g == 2): #this evaluates to False since 2 is not equal to '"2"'
  break

oh ok so is python automatically making the input an int? because if what you’re saying is right the code should not work yet it does. Can you explain how the code is still working?

For me the code does not work, I copy paste yours and it won’t accept the input correctly.

If the program you are running has the exact same code as what you posted, I have no clue how it could possibly run correctly.

I ran your code here: repl.it
and I am incapable of selecting heads or quitting the game.

Could you post a link to the lesson? Maybe I am missing something about the way the program is being run.

No, Python does not automatically convert the return value of input() to anything. input() returns a string, and it’s up to the programmer to change / validate the type as required.

I have the exact same experience:

Guess heads by entering 1 or tails by entering 2 for this coin flip.
1
you guessed tails.
The coin landed on Tails.
Sorry your guess was wrong.
You have guessed 1 times.
Would you would like to guess again enter 1 or enter 2 to exit?
2
Guess heads by entering 1 or tails by entering 2 for this coin flip.
2
you guessed tails.
The coin landed on Heads.
Sorry your guess was wrong.
You have guessed 2 times.
Would you would like to guess again enter 1 or enter 2 to exit?
1
Guess heads by entering 1 or tails by entering 2 for this coin flip.
1
you guessed tails.
The coin landed on Heads.
Sorry your guess was wrong.
You have guessed 3 times.
Would you would like to guess again enter 1 or enter 2 to exit?

You can see in the output that several of the ifs are failing - in the first run, I apparently guessed tails (I chose heads); the coin flip returned tails, yet I guessed wrong; and, finally, the game decided it wasn’t finished with me and so wouldn’t let me leave. :frowning:

Then I don’t think we’re running the same program. :slight_smile:

1 Like