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:
- 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.
- 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.
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.