I'm currently working on an assignment for my intro python class in school. The program is a "guess my number" variant, but its focus is on global variables and the try/except operation. I've managed to figure out most of it, but I'm having trouble calling my function that gives a random number. Choice 1 and 2 should use two different secret numbers, so I thought a function was the way to go, but I'm getting an error, TypeError: unorderable types: int() > function(). I've tried making everything int(), but that wasn't working. I'm not sure how to get my program to talk to my function correctly, and I cant find much info about how to fix unorderable types in our book or on the web. any suggestions or ideas would be helpful.
def secret (secret):
from random import randint
secret = randint (1, 10)
return secret
# first function will be for the menu( just like last homework assignment)
def menu_choice():
print("Guess the number")#any answers to this should be checked by try/except function for a number
print("0: Exit") #this should exit the loop and end the program
print("1: Guess") #this should go into the guess the number function/program
print("2: Get a new secret number")#not sure how this will work
return input("Enter your selection: ")
#setting my flags
loop = 1
choice = 0
while loop == 1:
# this try/except should weed out any non numeric answers. the choices will limit users
#to 0,1,2 and the else statement will remind them of their choices.
try:
choice = int(menu_choice())
except ValueError:
print(" The choice must be 0, 1, or 2. Please try again.")
if choice == 1:
guess = 0
while guess != secret:
guess = (input(" Please guess a number between 1 and 10. "))
#this try/except will do the same as above, but the first "if" statement below will handle
#any numbers outside the stated parameters of 1-10.
try:
guess = int(guess)
except ValueError:
print("That wasn't a number between 1 and 10. Please try again. ")
else:
if guess < 1 or guess > 10:
print(" The guess needs to be between 1 and 10. ")
elif guess > secret:
print("Too high!")
elif guess < secret:
print("Too low!")
print(" ****** That's correct! The number was", secret , "******")
elif choice == 2:
# not sure how to reset the randint, so i'm resorting to making choice 2 the
#same as choice 1. this isn't correct, because its calling the same randint,
#and getting the same secret number. need to try to make randint/secret
#a function so it erases the number after the choice is done with it.
guess = 0
while guess != secret:
guess = (input(" Please guess a number between 1 and 10. "))
try:
guess = int(guess)
except ValueError:
print("That wasn't a number between 1 and 10. Please try again. ")
else:
if guess < 1 or guess > 10:
print(" The guess needs to be between 1 and 10. ")
elif guess > secret:
print("Too high!")
elif guess < secret:
print("Too low!")
print(" ****** That's correct! The number was", secret, "******")
elif choice == 0:
print("Thank you for playing.")
loop = 0
else:
print("Please choose 0, 1, or 2. ")