Jan-04-2019, 01:44 PM
Hi. I'm new to Python and have created some code for a game. In the game, the user has 5 attempts to guess a number chosen at random by the computer. I have got it working as I wanted but just want some advice on if there is anything I could do to make it more streamlined and less long winded?
Advice much appreciated:
Advice much appreciated:
from time import sleep from random import randint player = input('What is your name? ') print('Hello', player + '!') ; sleep(1) print("Let's play a game!") ; sleep(2) print("I'm going to pick a number between 0 and 9.") ; sleep(2) print("I'll give you 5 guesses to get it right.") ; sleep(2) print("Good Luck!!") ; sleep(2) number = (randint(0,9)) pick = int(input('Pick a number between 0 and 9: ')) print('Your guess is:', pick) ; sleep(1) lives = 5 count = 1 while pick != number: lives -= 1 if(pick >number): print("Sorry that number is too high! Try again! Lives left =", lives) ; sleep(1) pick = int(input('Pick a number between 0 and 9: ')) ; sleep(1) else: print("Sorry that number is too low! Try again! Lives left =", lives) pick = int(input('Pick a number between 0 and 9: ')) ; sleep(1) count += 1 if (count == 5) and(pick != number): print('Sorry, you are out of lives! Game over!!') ; sleep(1) break while pick == number: if (lives == 1): print('Well done! You got it with', lives ,'life left!') break else: print('Well done! You got it with', lives ,'lives left!') break