# Hey, I'm new to python so don't judge.

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jan 3 21:16:58 EST 2017

```On Wednesday 04 January 2017 13:24, Callum Robinson wrote:

> On Wednesday, January 4, 2017 at 3:05:48 PM UTC+13, MRAB wrote:
>> On 2017-01-04 01:37, Callum Robinson wrote:
>> > On Wednesday, January 4, 2017 at 12:49:28 PM UTC+13, Callum Robinson
>> > wrote:
>> >> Im doing a new task from my teacher but i can't seem to find what is
>> >> wrong with this code. Can anyone help?
>> >>
>> >> #mynumber.py
>> >> # this game uses a home made function
>> >> import random
>> >>
>> >> #think of a number
>> >> computer_number = number.randint(1,100)
>> >>
>> >> #create the function is_same()
>> >> def is_same(target, number:
>> >>         if target == number:
>> >>             result="win"
>> >>         elif target > number:
>> >>             result="low"
>> >>         else:
>> >>             result="high"
>> >>         return result
>> >>
>> >> # start the game
>> >> print("hello. \nI have thought of a number between 1 and 100.")
>> >>
>> >> #collect the user's guess as an interger
>> >> guess = int(input("Can you guess it? "))
>> >> #Use our function
>> >> higher_or_lower = is_same(computer_number, guess)
>> >> #run the game untill the user is correct
>> >> while higher_or_lower != "win"
>> >>     if higher_or_lower == "to low"
>> >>          guess = int(input("Sorry, you are too low. Try again."))
>> >>     else:
>> >>         guess = int(input("Sorry your are too high. Try again."))
>> >>
>> >>     higher_or_lower = is_same(computer_number, guess)
>> >>
>> >> #end of game
>> >
>> > Hey again, i'm sorry for bothering you with so many questions i just did
>> > not want to keep asking my teacher who is on holiday these.
>> >
>> > I have another issue where the code runs but i can guess every number from
>> > 1-100 but it always says Sorry your are too high. Try again. I don't
>> > understand what i have done to cause this.
>> >
>> What values can 'is_same' return?
>>
>> Which of those values are you checking for in the loop?
>
> I'm sorry but i do not completely understand what you are stating

That's okay, you're still learning :-)

We've all been where you are, even if some of us have forgotten what its like.

Look at your is_same() function. It can return three different things, which
gets stored in the higher_or_lower variable:

- "win"
- "low"
- "high"

But now look at how you check the result:

while higher_or_lower != "win"
if higher_or_lower == "to low"
guess = int(input("Sorry, you are too low. Try again."))
else:
guess = int(input("Sorry your are too high. Try again."))

I see a typo that will prevent your code from running: you are missing a colon
after the first line, it should say:

while higher_or_lower != "win":

Likewise for the "if ..." line, it also needs to end with a colon.

So the first thing is that when asking for help, be *extra careful* that the
code you show us is the same as the code you are actually running. (Otherwise
we waste our time trying to debug code that you aren't even using!) You should
always COPY AND PASTE your code, not retype it.

But let's assume that your actual code does include the needed colons, so it
will run. What values do you check for?

- "to low"

and that's it. Look again at the values that higher_or_lower can actually be.
Is there any way that higher_or_lower gets the value "to low"?

No. Remember that Python can't read your mind and realise that when you check
for "to low", you actually mean just "low". So there is no way that the first
if... clause will be triggered, so it always falls through to the else clause
and prints "Sorry your are too high. Try again."

(P.S. you mean "Sorry you are too high", not "your are".)

--
Steven
"Ever since I learned about confirmation bias, I've been seeing it everywhere."
- Jon Ronson

```