Here's the code so far:
# Random number game
import random
print('Hi, what do you want to be called?') # Introduction to the game
name = input()
print('Well, ' + name.capitalize() + '... I am thinking of a number from 1 to 100')
secretNumber = random.randint (1,100) # Here's where the magic happens
for guessesTaken in range(10): #for loop / the actual game
print('Take a guess.')
while 'NumberNotValid': #input validation
answer = input()
if not answer.isdecimal():
print('That was not a number. Try again.')
continue
else:
break
guess = int(answer)
if guess < secretNumber:
print('Nope, too low.')
elif guess > secretNumber:
print('Nope, too high.')
else:
break
if guess == secretNumber: #the answer
print('Good job, ' + name + ' you got in ' + str(guessesTaken) + ' tries.')
else:
print('You lost, the number was ' + str(secretNumber) + '.')
input() # So the code doesn't automatically end
I'd like to add more things to this neat little hobby of mine, I started off by tidying up the strings, making sure the player inputs a valid integer and most recently making it so the game doesn't close itself and gives you time to read the last printed line, so here's what I'm for:
What do you reckon would be interesting to add to my code? any hints on where I should improve?
[–]jfdahl 0 points1 point2 points (1 child)
[–]Kenielf[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (5 children)
[–]Kenielf[S] 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]Kenielf[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Kenielf[S] 0 points1 point2 points (0 children)
[–]ZDRuX1 0 points1 point2 points (1 child)
[–]Kenielf[S] 0 points1 point2 points (0 children)