0

One of the exercises in my assignment is to make program which guesses a number:

min=1
max=10
count=1

answer=input("Think of af number between "+str(min)+" and "+str(max)+" and then press enter ")

while answer!="correct":

    if min>max:
        print("\nYou are cheating! Good bye.")
        break

    else:
        guess=(min+max)//2
        answer=input("My no."+str(count)+" guess is "+str(guess)+" - enter 'higher', 'lower' or 'correct': ")

        if answer=="correct":
            print("I guessed it!")

        elif answer=="higher":
            min=guess+1
            count+=1

        elif answer=="lower":
            max=guess-1
            count+=1

        else:
            print("\nCouldn't recognize your input, please try again.")

We have just been taught about exceptions, but I'm not really sure if it would make sense to use an exception in this case, maybe instead of the else statement?

Ellis
  • 39
  • 1
  • 5
  • You might find it useful to read [this SO thread](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Mr. T Feb 23 '18 at 15:46
  • if it's fine as it is you don't need to bother with exceptions. You could catch an exception when for examples sake you want an integer off the user but they provide a string, giving you a TypeError which you can then catch to print out something prettier than just a straight up crash. – L_Church Feb 23 '18 at 15:47
  • Where do you ever assign `answer = "correct"`? – pstatix Feb 23 '18 at 16:15

0 Answers0