Skip to content

Instantly share code, notes, and snippets.

@Elijah-trillionz
Created November 25, 2021 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Elijah-trillionz/be5edce82f4d896854bd6611caed8d10 to your computer and use it in GitHub Desktop.
Save Elijah-trillionz/be5edce82f4d896854bd6611caed8d10 to your computer and use it in GitHub Desktop.
guessing game for computers
# guess the number game. (computers)
import random as rd
def guess_number():
user_input = int(input('Enter a number for computer to guess btw 1 and 10 (inclusive): '))
attempts = 2
while attempts >= 0:
is_wrong = compare_guess(user_input, attempts)
if is_wrong:
print(is_wrong)
else:
print('Guessed right')
break
attempts -= 1
else:
print('Computer failed')
def compare_guess(user_input, attempts):
if attempts < 2:
print('trying again...')
else:
print('guessing...')
# a minor delay, just to make the game interesting
i = 0
while i < 50000000:
i += 1
computer_input = rd.randint(1, 10)
if computer_input == user_input:
return False
elif computer_input > user_input:
return f'Too low. Computer has {attempts} more trials'
else:
return f'Too high. Computer has {attempts} more trials'
guess_number()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment