0

I've been trying to solve the issue with guessing the number program, The first number the program has to print Ans which is (Startlow + Starthigh)/2 and then Ans gets updated depends on the input

I can't figure out why my while loop keeps waiting for the input for at least 2 times until it prints the results even if I press l or h (unless I press c) which breaks the loop

Startlow = 0
Starthigh = 100
Ans = (Startlow + Starthigh)/2
print("Please think of a number between 0 and 100!")

while True:
    print("Is your secret number " + str(int(Ans)))
    if input() == "c":
        print("Game over,Your secret number was: "+str(int(Ans)))
        break
    elif input() == "l":
        Startlow = Ans
        Ans = (Startlow + Starthigh)/2
    elif input() == "h":
        Starthigh = Ans
        Ans = (Startlow + Starthigh)/2
    else:
        print("Sorry, I did not understand your input.")

any help appreciated :)

1
  • 2
    Every time you call input(), Python waits for input. Why are you surprised? Commented Sep 6, 2020 at 4:57

2 Answers 2

2

You should be asking for input once in the loop, and then comparing that answer to the items you want.

You are instead requesting a (potentially different) answer at each of your conditionals.

The number of questions asked depends on how many conditionals you fall through.

Sign up to request clarification or add additional context in comments.

Comments

1

Just do:

x = input()
if x == "c":
  #And so on...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.