Answer to Question #240476 in Python for Caleb

Question #240476

Roll the Dice, where each dice has from 1 to 6 dots on each side. Two dice will be rolled. Use the random function to generate a random integer between the beginning and ending range of dot possibilities.

Your program should generate a random number. Then display a message to prompt a player to enter his/her name and then guess the number. They have 5 chances to get the number correct. If they guess the right one, display to them that they guessed the correct number in the number of guesses. If they did not guess itin 5 attempts, display a message telling them they were not successful in guessing the correct number and tell them which number it was. Allow the player to play again.


1
Expert's answer
2021-09-22T06:28:43-0400
import random
result = random.randint(2,12)
name = str(input("Enter your name: "))
attempt = 0
while attempt < 5:
  prediction = int(input("Try to predict: "))
  if attempt < 4 and prediction == result:
    print(f"{name}, you guessed {result} using {attempt+1} attempts")
    break
  elif attempt < 4 and prediction != result:
    print("Incorrect")
    attempt+=1
  elif attempt == 4:
    print(f"{name}, you were not successful in guessing the correct number, the correct number is {result} ")
    break

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS