I am fairly new to programming, and I recently finished part of one of my projects, a guessing game. The part I completed was the game mode where the player guesses the computer’s number. I’m quite proud of it, but I have a strong feeling that it is hugely suboptimal, as I only really know the basics so far. I would like some feedback how to not only improve this program, but all programs I make in the future. Thank you!
P.S. Please be specific when giving feedback, because there may be some terms that I do not know.
My code:
import random
import sys
import time
#controls typing speed (I got this from online)
typing_speed = 150 #wpm
def slow_print(t):
for l in t:
sys.stdout.write(l)
sys.stdout.flush()
time.sleep(random.random()*10.0/typing_speed)
print('')
#asks difficulty
def ask_guess_difficulty():
slow_print('Alright! I'll pick a number from 1 to 100, and you try to guess it!')
slow_print('''You can do easy mode (E), with infinite guesses; normal mode (N), with 10 guesses;
hard mode (H), with 5 guesses; or custom mode (C), where you pick how many guesses you get!''')
guess_difficulty = input()
guess_difficulty_lower = guess_difficulty.lower()
if guess_difficulty_lower == 'e':
guesses = 1000
return guesses
elif guess_difficulty_lower == 'n':
guesses = 11
return guesses
elif guess_difficulty_lower == 'h':
guesses = 6
return guesses
elif guess_difficulty_lower == 'c':
slow_print('How many guesses do you want?')
guesses = input()
if guesses.isnumeric() == True:
guesses = int(guesses) + 1
slow_print('Ok! You will have ', str(guesses - 1), ' guesses!')
return guesses
else:
slow_print('I don't understand. Please input a number for the amount of guess that you want.')
else:
slow_print('''I don't understand. Please input "E" for easy mode, "N" for
normal mode, "H" for hard mode, and "C" for custom mode''')
#plays the gamemode where the player guesses the computer's number
def player_guess(guesses):
guesses = int(guesses)
player_guess_win = False
slow_print('Ok! I'm thinking of a number from 1 to 100')
slow_print('You'll have ', str(guesses - 1), ' guesses to get it.')
slow_print('Go ahead and guess when you're ready.')
total_tries = 0
player_guess_num = random.randint(1,100)
last_guess = None
low_high = None
while player_guess_win == False and guesses > 0:
guesses -= 1
total_tries += 1
if int(guesses) > 0:
current_guess = input()
if current_guess.isnumeric() == True and int(current_guess) <= 100 and int(current_guess) >= 1:
if int(current_guess) == last_guess:
slow_print('That was your last guess silly! Make sure to enter a new number!')
guesses += 1
continue
if low_high == 'high':
if int(current_guess) > last_guess:
slow_print('Your guess is...')
if int(current_guess) == player_guess_num:
slow_print('CORRECT!')
slow_print('Congratulations! You took ',str(total_tries),' guesses to get it right!')
slow_print('Do you want to play again? (Please input "yes" or "no".)')
while True:
yn_choice = input()
if yn_choice == 'yes' or yn_choice == 'y':
slow_print('Ok, let's do it!')
guess_the_number_start()
break
elif yn_choice == 'no' or yn_choice == 'n':
slow_print('Ok!')
exit()
else:
slow_print('I don't understand. Please input "yes" or "no".')
else:
if int(current_guess) < player_guess_num:
slow_print('Too low.')
last_guess = int(current_guess)
low_high = 'high'
continue
else:
slow_print('Too high')
last_guess = int(current_guess)
low_high = 'low'
continue
else:
slow_print('Oops! Make sure you enter a number that is greater than your last guess!')
guesses += 1
continue
elif low_high == 'low':
if int(current_guess) < last_guess:
slow_print('Your guess is...')
if int(current_guess) == player_guess_num:
slow_print('CORRECT!')
slow_print('Congratulations! You took ',str(total_tries),' guesses to get it right!')
slow_print('Do you want to play again? (Please input "yes" or "no".)')
while True:
yn_choice = input()
if yn_choice == 'yes' or yn_choice == 'y':
slow_print('Ok, let's do it!')
guess_the_number_start()
break
elif yn_choice == 'no' or yn_choice == 'n':
slow_print('Ok!')
exit()
else:
slow_print('I don't understand. Please input "yes" or "no".')
else:
if int(current_guess) < player_guess_num:
slow_print('Too low')
last_guess = int(current_guess)
low_high = 'high'
continue
else:
slow_print('Too high')
last_guess = int(current_guess)
low_high = 'low'
continue
else:
slow_print('Oops! Make sure you enter a number that is less than your last guess!')
guesses += 1
continue
else:
slow_print('Your guess is...')
if int(current_guess) == player_guess_num:
slow_print('CORRECT!')
slow_print('Congratulations! You took ',str(total_tries),' guesses to get it right!')
slow_print('Do you want to play again? (Please input "yes" or "no".)')
while True:
yn_choice = input()
if yn_choice == 'yes' or yn_choice == 'y':
slow_print('Ok, let's do it!')
guess_the_number_start()
break
elif yn_choice == 'no' or yn_choice == 'n':
slow_print('Ok!')
exit()
else:
slow_print('I don't understand. Please input "yes" or "no".')
else:
if int(current_guess) < player_guess_num:
slow_print('Too low')
last_guess = int(current_guess)
low_high = 'high'
continue
else:
slow_print('Too high')
last_guess = int(current_guess)
low_high = 'low'
continue
else:
slow_print('I don't understand. Please enter a number from 1 to 100.')
guesses += 1
continue
else:
slow_print('You ran out of guesses. My number was ',str(player_guess_num), '.')
slow_print('Would you like to play again?')
slow_print('(Please input "Yes" or "No".)')
play_again_gtn = input()
play_again_gtn_lower = play_again_gtn.lower()
if play_again_gtn_lower == 'y' or play_again_gtn_lower == 'yes':
slow_print('Oki doki!')
continue
elif play_again_gtn_lower == 'n' or play_again_gtn_lower == 'no':
slow_print('Oki doki! Bye!')
exit()
#computer guesses your number (not finished yet
def computer_guess():
slow_print('Ok, I'll guess!')
slow_print('First, we need to figure out the span of numbers I'll be guessing')
slow_print('What should the minimum be? The number will not be able to go any lower than this.')
#begins the program
def guess_the_number_start():
slow_print('Do you want to guess my number, or should I guess yours?')
slow_print('(If you want to guess, input "I". If I should guess, input "U".)')
who_guess = input()
if who_guess.lower() == 'i':
guesses = ask_guess_difficulty()
player_guess(guesses)
elif who_guess.lower() == 'u':
computer_guess()
else:
slow_print('I don't understand. Please input "I" if you should guess and input "U" if I should guess.')
#loops the program
while True:
guess_the_number_start()
ADAM WELLER-FAHY is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.