0

I'm a complete beginner in python. I apologize if this question is repetitive or if I seem like an idiot.

high = 100
low = 0
correct = False
response = ""
user_number = input("Please think of a number between 0 and 100!")
while (response != "c"):
    guess = int((high + low)/2)
    print("Is your secret number", guess, "?")
    response = input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly")
    if not  (response == "h" or response == "c" or response == "l"):
        print("Sorry, I did not understand your input.")
    elif (response is "h"):
        high = guess
    elif (response is "l"):
        low = guess

print ("Game over. Your secret number was:", guess)

This is a fellow course mate's code.

Can someone explain to me the purpose of this section? I would really appreciate your help.

correct = False
    response = ""
    user_number = input("Please think of a number between 0 and 100!")
    while (response != "c"):
Aqib
  • 27
  • 2
  • An aside (even though it's your fellow course mate's code): doing `response is "h"` and `response is "l"` is considered bad practice for strings. See [String Comparison in Python: is vs. ==](https://stackoverflow.com/questions/2988017/string-comparison-in-python-is-vs). – TrebledJ Feb 27 '19 at 14:12

2 Answers2

0

I can only imagine this is his 'keyword' in order to exit the program. So typing c would just not run anything. Although anything other than a number will throw an error here due to the first line performing calculations and also casting to int

Nordle
  • 2,915
  • 3
  • 16
  • 34
0

So, in general this asks for a number (though it never uses it again), and then guesses a number (50 in the first iteration). The user is asked whether the guess is above or below the initial number. Based on the answer, it makes another guess. This continues until the the user enters c

To address your more specific question:

  • correct = False is useless because correct variable is never used again

  • response = "" holds the input of the user

  • user_number = input("Please think of a number between 0 and 100!") gets the user input.

    • Actually user_number is not used anywhere else, so you can just write input("Please think ...")
  • while (response != "c") loops until user input from response = input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly") equals to c

Hope this helps.

KarimShn
  • 14
  • 4