why this python code runs normally although the output is incorrect
low = 1
high = 1000
print(f"I will guess a value from {low} and {high}"
"press 'h' or 'l' or 'c'for complete anything else to exit")
guesses = 1
while True:
mid = low + (high - low) // 2
print("is this is your guess {}".format(mid))
inp = input()
if inp == 'h':
low += 1
elif inp =='l':
high -= 1
elif inp == 'c':
print('got it')
else:
break
but when i add just this line of code it says invalid syntax and breaks
low = 1
high = 1000
print(f"I will guess a value from {low} and {high}"
"press 'h' or 'l' or 'c'for complete anything else to exit")
guesses = 1
while True:
mid = low + (high - low) // 2
print("is this is your guess {}".format(mid))
inp = input()
if inp == 'h':
low += 1
elif inp =='l':
high -= 1
elif inp == 'c':
print('got it')
guesses += 1
else:
break
so why guesses += 1 just breaks everything it is just a counter it does not have any role in the code except count for how many times i looped
- if you can also tell me why this code doesn't print print predicted values will be appreciated the program is supposed to be a binary search program .
Aucun commentaire:
Enregistrer un commentaire