I just started learning Python this week and put this together for my first program (outside of a Hello World one). Does anyone have any feedback/suggestions for improvement for this? I've tried to forsee potential user input issues but I'd love to hear some thoughts from people who know what they are doing!
import random
guessesTaken = 0
# sets the guess counter
print ('Hello! What is your name?')
name = input()
# intro for user
print ('Hi, ' + name + ', I am thinking of a number between 1 and 100.' "\n" 'Do you want to try to guess my number? Please type yes or no.')
play = input()
# user inputs whether or not they want to play
while play != 'yes' and play != 'no':
print ("I'm sorry, I didn't understand that. Please type yes or no.")
play = input ()
# provision for invalid user input: if the input is not yes or no, they will be prompted to re-enter
while play == 'yes':
# the game continues
number = random.randint (1,100)
# the number will be between 1 and 100
while guessesTaken < 20:
print ('Take a guess!')
# sets a max of 20 guesses
try:
guess = int(input())
except:
print ("I'm sorry, I didn't understand that. Please type a number between 1 and 100.")
continue
# provision for invalid input - if input is not an integer, user will be prompted to re-enter input
guessesTaken += 1
# adds to the guess counter
if guess < number:
print ('Your guess is too low!' "\n" 'Try again. You have ' + str(20-guessesTaken) + ' guesses left.')
elif guess > number:
print ('Your guess is too high!' "\n" 'Try again. You have ' + str(20-guessesTaken) + ' guesses left.')
elif guess == number:
break
# if the guess is too high or too low, the loop continues. If the user guesses right, the loop ends
if guess == number:
print ('You got it right!' "\n" 'It only took ' + str(guessesTaken) + ' guesses!')
# in case of correct guess within the allowed number of tries
else:
print ('Sorry, the number I am thinking of is ' + str(number) + '.')
# if the user exceeds the number of guesses allowed
guessesTaken = 0
# resets guess counter
print ('That was fun, ' + name + '! Want to play again?' "\n" 'Please type yes or no.')
play = input ()
# asks if user wants to play again, with provision for invalid input
if play == 'no':
print ("That's too bad, see you next time!")
# ends the game
while
loop and everything following it correct? – Graipher 18 hours ago