May-19-2018, 12:01 PM
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.
Thanks
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