Wednesday, 15 February 2012

Python: input function not working as expected -


Say SECRET_NUMBER = 77. I want the user to point the function to the user until the secret number is estimated. However, 'Your estimate is very low' or 'Your estimate is high' is not working properly if I enter estimate_number (4), then the guess is very low, but if I put it in the next 100, So it still says that my guess is very low. What's wrong with my function?
  def estimate_number (num): '' '(int) - & gt; Do not type any whether the secret number was guessed or not indicated, if the number was not estimated. Until the secret number is approximated, the user will give this signal for a number. & Gt; & Gt; & Gt; Guess_number (50) Your guess was very low! '' 'While num! = SECRET_NUMBER: If the number is & lt; SECRET_NUMBER: Print ('Your guess was too low!') Input ('a number appears:') Alip no & gt; SECRET_NUMBER: Print ('Your guess was very high!') Input ('A number looks:') Other: Print ('You guessed it!')    

input () returns the user has entered. You are not returning the function returns; It is abandoned instead

Store it in your variable:

  num = input ('seems a number:')   

Want to convert it to an integer; input () returns a string:

  num = int (input ('seems a number:'))   

You only need to ask it once for every effort:

  while num! = SECRET_NUMBER: If the number is & lt; SECRET_NUMBER: Print ('Your guess was very low!') Alief n & gt; SECRET_NUMBER: Print ('Your guess was very high!') Num = int (Input ('a number seems:')) Other: Print ('You guessed it!')   

See besides this

No comments:

Post a Comment