description:
Ask user to predict dice number
Check if entry is from range 1 to 6 only, if invalid ask again.
If valid do this:
# Computer Science Indipendent Project # 1 -- Coin Flip
# Source : <https://discuss.codecademy.com/t/computer-science-independent-project-1-coin-flip/419951>
# User can choose a game. They should be able to play until they want and they can switch the game from time to time.
# Score for the game session will be tallied.
import random
score = 0
#Function to choose a game between dice and coinflip
def game_options():
#Validation of the input to gate some possible error
#I just learned this here : <https://www.includehelp.com/python/asking-the-user-for-input-until-a-valid-response-in-python.aspx>
while True:
try:
game = int(input("\\nChoose your game! Enter 1 for Dice Game or 2 for CoinFlip >> "))
if game == 1 :
dice()
break
elif game == 2 :
coin_flip()
break
else :
print("Please choose the corect number\\n1 - Dice Game or 2 - Coin Flip Game\\n")
except ValueError :
print("Try Again with a correct number! \\n")
continue
#Function below will trigger the dice game
def dice():
print ("\\n\\nWelcome to the Dice Game!\\n\\n")
global score #score is set to global to combine scores from dice and coin flip
while True:
try:
predict = int(input("To earn 1 point, you must predict the dice roll number.\\nEnter a number from 1 to 6 : "))
if predict >= 1 and predict <= 6 :
break
else :
print("\\n!!! Please enter a number from 1 to 6 only! \\n")
except ValueError :
print("**Enter a correct value!**")
continue
# at the end of input validation, we'll roll the dice and show the result accordingly.
dice_num = random.randint(1,6)
if predict == dice_num :
score += 1
print (f"\\n*** Congratulations *** you predicted it correctly.\\nOur Dice rolled > {dice_num}\\nYOUR SCORE NOW IS: {score}! \\n")
# call function to ask user if they want to continue or exit
ans = play_again()
if ans == True:
dice()
else:
exit()
else :
print (f"\\nYou missed the right one. \\nThe dice rolled {dice_num}\\n")
# call function to ask user if they want to continue or exit
ans = play_again()
if ans == True:
dice()
else:
exit()
# Function below will trigger the coin flip game. Everything works as the same logic as the dice game
def coin_flip():
print ("Welcome to the Coin Flip Game!\\n\\n")
global score
coin_face = ["Heads", "Tails"]
while True:
try:
predict = input("To earn 1 point, you must predict the coin's face after we flip it.\\nEnter 'h' for heads or 't' for tails : ").lower()
if predict == "h":
predict = 0
break
elif predict == "t":
predict = 1
break
else :
print("\\n!!! Please enter h or t only. \\n")
except ValueError :
print("**Enter a correct value!**")
continue
i = random.randint(0,1)
if predict == i :
score += 1
print (f"\\n*** Congratulations *** you predicted it correctly.\\nOur coin flipped the > {coin_face[i]}\\nYOUR SCORE NOW IS: {score}! \\n")
ans = play_again()
if ans == True:
coin_flip()
else:
exit()
else :
print (f"\\nYou missed the right one. \\nThe coin flipped {coin_face[i]}\\n")
ans = play_again()
if ans == True:
coin_flip()
else:
exit()
# Function below will ask user if they want to play again, switch games or exit the program
def play_again():
while True:
try:
ans = input("Would you like to play again?\\nPlease enter 'y' for yes / 'b' to go back to the game options 'x' to exit\\nEnter y, b or x here >> ").lower()
if ans == "y":
return True
elif ans == "b":
game_options()
elif ans == "x":
print("\\nThanks for playing! Goodbye!")
return False
break
else:
print("Please enter y to continue playing, b to go back to main or x to exit >> ")
except ValueError:
print("Try choosing y or b or x! ")
continue
# START OF THE GAME
game_options()
# From
if ans == "y":
# To
if ans.lower() == "y":