Python Forum
Guess the dice roll mini-game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Guess the dice roll mini-game
#1
Hey everyone, I just finished my first program, I've been learning python for about 3 days now.

I am wondering if there are ways I can make my code smaller / optimized more.

import random

userScore = 0
userTries = 7
print ("Welcome to the dice game! where you guess the number thats about to be rolled. Get a score of 3 to win!")
print ("Your score is:", userScore, "\n")

while (userScore <= 3):
	while (userTries >= 0):

		diceNum = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)

		print ("Tries left:", userTries, "\n")
		userGuess = input ("Enter your guess: " )

			# checks if users guess is NOT in the diceNum list
		if (int(userGuess) not in diceNum):
			print (userGuess, "\nis not a valid guess, try again.")
			userTries -= 1

			#if the user guess IS IN the diceNum list it continues with the next statement
		else:
			diceNum = random.choice(diceNum)
			print ("\nThe dice rolled...", diceNum)

			#Checks if the random generated number matches the guess
		if (int(diceNum) == int(userGuess)):
			userScore += 1
			print ("You guessed it right! +1")
			print ("Your score:", userScore)

			# checks if user guessed right if no take away a point
		elif (int(diceNum) != int(userGuess)):
			userTries -= 1
			print ("You guessed wrong try again -1 try")
			print ("Your score: ", userScore)

			#only solution i found to not get stuck in one of the while loops when user tries = 0 or score = 3
		if (userTries == 0):
			break
		elif (userScore == 3):
			break
	if (userTries == 0):
		break
	elif (userScore == 3):
		break


print ("Goodbye!")
Appreciate any feedback!

Thanks
Reply


Messages In This Thread
Guess the dice roll mini-game - by tawnnx - May-19-2018, 12:01 PM
RE: Guess the dice roll mini-game - by ljmetzger - May-19-2018, 04:22 PM
RE: Guess the dice roll mini-game - by tawnnx - May-19-2018, 10:28 PM
RE: Guess the dice roll mini-game - by volcano63 - May-20-2018, 12:11 AM
RE: Guess the dice roll mini-game - by malonn - May-21-2018, 11:30 PM
RE: Guess the dice roll mini-game - by volcano63 - May-22-2018, 12:25 PM
RE: Guess the dice roll mini-game - by malonn - May-22-2018, 02:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Guess your number joe_momma 0 2,465 Oct-23-2020, 02:53 AM
Last Post: joe_momma
  Guess my number foksikrasa 0 2,408 May-28-2020, 04:12 PM
Last Post: foksikrasa
  SKUNK Dice Game ProntName 9 6,569 Apr-19-2020, 12:15 PM
Last Post: eXcalibur432
  guess my number GAME ronblue77 2 2,795 Nov-24-2019, 04:23 PM
Last Post: CodingStranger
  Mastermind/Guess the Code Game JoeLamond 5 12,333 Jan-14-2019, 06:34 PM
Last Post: steve_shambles
  5 mini programming projects for the python beginner kerzol81 4 36,259 Sep-26-2017, 02:36 PM
Last Post: VeenaReddy
  Mini-Web Framework nilamo 12 9,772 Jun-15-2017, 05:32 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020