Dear All,

I am not only new to python, but completely new to programming in general. So please don't laugh (too much) of this certainly dumb question.

I was going through the first pages of "A Byte of Python" and decided to change a bit one of the models and came up with this:

``````#!\bin\python3
# Filename: printMax.py

while True:

x = (input('Give a value for \'a\': '))

if x == 'quit':
break

y = (input('Give a value for \'b\': '))

if y == 'quit':
break

def printMax(a, b):

if a > b:
print(a, 'is maximum')

elif a == b:
print(a, 'is equal to', b)

else:
print(b, 'is maximum')

printMax(x,y)

print('Done')``````

I ran it and I was so happy with it, but then... why on Earth does it tell me that "5" is higher than "24"? Or why does it tell me that "5" is higher than "45"?? Or why "5" is higher than "13"? :-O

I suspect I should somehow specify I am working with decimal integers. Is it so?

Thanks in advance for any possible help.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by yeticannotski

ran it and I was so happy with it, but then... why on Earth does it tell me that "5" is higher than "24"? Or why does it tell me that "5" is higher than "45"?? Or why "5" is higher than "13"?

Because you are comparing string and not integer.
Python 3 has only `input()` and it return a string,but you can choose what datatype it shall return with `int() float()` .

For beginner there is a massive amout of tutorials/books/forum help that are in python 2.x this is important stuff for learing python.
If you learn python 2.x you learn of course python 3.x,have both installed is a good chooice.
Most pepole still use python 2.x in wait for many 3 part moduls to be rewritten in python 3.

``````#python 3
>>> x = input('Give a value for \'a\': ')
>>> Give a value for 'a': 5
>>> type(x)
<class 'str'>

>>> x = int(input('Give a value for \'a\': '))
>>> >>> Give a value for 'a': 3
>>> type(x)
<class 'int'>

>>> #Or use float for decimal number
>>> x = float(input('Give a value for \'a\': '))
>>> Give a value for 'a': 5.3
>>> type(x)
<class 'float'>

>>> #You can also convert later in code
>>>  if float(a) > float(b):``````

Edited by snippsat: n/a

Thank you so much. Really. It was very frustrating to stumble on the very first steps.

As per the 2.x vs. 3 issue, my idea was: it will take so many years before I will be able to do anything actually useful with Python and before I will need advanced modules or will collaborate with active projects that, by then, Python 3 will probably have gotten the upperhand.

Anyway, your point is absolutely valid and I will consider using 2.x as my starting point.