Question:
Greetings!
I'm starting with Python 3.5. I have written this code and I have no idea why it doesn't work, the version of this code in 2.7 works but when moving to 3.5 it doesn't and I suspect it is because of some number handling in this version. It doesn't matter if you press "h" or press "l", it always returns the same thing.
It is a code that tries to "guess" what number you have thought (from the edx course).
lowest = 0
highest = 100
guess = 50
while True:
print("Is your secret number %d?" %(guess))
answer = str(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 answer == "h":
highest = guess - 1
guess = (highest - lowest)/2
elif answer == "l":
lowest = guess + 1
guess = (highest - lowest)/2
elif answer == "c":
break
else:
print("Sorry, I did not understand your input.")
print("Game over. Your secret number was: %d" %(guess))
Thanks and greetings to all!
Answer:
There are a few differences between Python 2.x and Python 3.x. In general, if you're just starting out, you should use Python 3.x unless you have a special requirement to do so in Python 2.x. Going to the code, there are several differences that make the code act differently.
In Python 3.x division by integers works as expected, whereas in Python 2.x division by integers returns the integer part of the result:
# Python 2.x
>>> print(1/2)
0
# Python 3.x
>>> print(1/2)
0.5
To make division in Python 2.x work as expected, you can convert one of the numbers in the division to a decimal, or you can import the division functionality as it works in Python 3.x:
# Python 2.x (opción a)
>>> print(1./2) # print(1/2.)
0.5
# Python 2.x (opción b)
>>> from __future__ import division
>>> print(1/2)
0.5
In Python 3.x, to do a division between integers and have the result be an integer, you can use the //
operator:
# Python 3.x
>>> print(1//2)
0
On the other hand, in Python 2.x there is input
and raw_input
. The first evaluates the expression you insert, being able to execute code with all the danger that comes with allowing a user to execute code. This is why in Python 3.x input
was removed and raw_input
was renamed ínput
. So in Python 3.x you only have input
available which works like raw_input
in Python 2.x. So if you're on Python 2.x, never ever use input
unless you know what you're doing and have good reasons to do so.
Specifics of your code, when you use quotes or double quotes you don't need to 'escape' the quotes like when you do '\'h\''
. You can combine both quotes and double quotes as long as you are consistent. For example, the following is valid:
print("Hola, 'estoy entrecomillado'.")
If you open and close with double quotes you can use quotes in your string. The same thing happens if you open and close with quotes and use double quotes in your string:
print('Hola, "estoy entrecomillado".')
But if you mix them it will lead to errors.
Going to your code, it doesn't work for me neither in Python 2.x nor in Python 3.x and since you're using different things it's normal that it doesn't act the same. Keeping the code as you've written (and making it compatible with Python 2.x and 3.x) the following could be done:
from __future__ import division # para que la división actúe igual en ambas versiones
import sys
# Esto lo hacemos para saber qué input vamos a usar en ambas versiones
# creamos un alias para saber lo que estamos usando y no meter condiciones
# en medio del código
if sys.version_info.major == 3:
my_input = input
else:
my_input = raw_input
lowest = 0
highest = 100
guess = 50
while True:
print("Is your secret number %d?" %(guess))
answer = my_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 answer == "h":
highest = guess
guess = highest - (highest - lowest) // 2 # este es un error en tu algoritmo
elif answer == "l":
lowest = guess
guess = lowest + (highest - lowest) // 2 # este es un error en tu algoritmo
elif answer == "c":
print("Game over. Your secret number was: %d" %(guess))
break
else:
print("Sorry, I did not understand your input.")
The ideal would be to also use limit the value of the value that the user can enter, but I'll leave that to you.