Python Forum
Single digits seem to be greater than double digits - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Single digits seem to be greater than double digits (/thread-31062.html)



Single digits seem to be greater than double digits - Frosty_GM - Nov-20-2020

Hello, I am new to Python and I've been playing around with the 'Hello World' script that everyone probably starts with

I wanted to see if i could you 'if' properly so I made 2 'if' lines that would print a different response depending on 'age=input()'

I created a simple equation to write if the age=input() was <'18'. It simple finds the different between 'age=input()' and '18' to print how long is left until 18.

The trouble is, if 'age=input()' is a single digit int such as 4 or 9, the script will print the line for 'age>='18'. Almost like a single digit int is greater than '18'

Here is the code:

print('hello world!')
print('What is your name?')
name=input()
print('That is a nice name, '+name)
print('How old are you, '+name+' p.s. put a zero infront of a single digit')
age=input()
if age>='18':
print("if you're "+age+", that means it's beer o'clock!")
diff=str(18-(int(age)))
if age<'18':
print('That means you have to wait '+diff+' years to have a drink on me!')

Here is the output if age is a single digit int:

hello world!
What is your name?
G
That is a nice name, G
How old are you, G p.s. put a zero infront of a single digit
2
if you're 2, that means it's beer o'clock!

Whereas is you put a '0' infront of the single digit int, it works:

hello world!
What is your name?
G
That is a nice name, G
How old are you, G p.s. put a zero infront of a single digit
02
That means you have to wait 16 years to have a drink on me!

Any help is appreciated!


RE: Single digits seem to be greater than double digits - sandeep_ganga - Nov-20-2020

See below, i used to typecast to str only at print, rest comparisons in int.

print('hello world!')
print('What is your name?')
name=input()
print('That is a nice name, '+name)
print('How old are you, '+name+' p.s. put a zero infront of a single digit')
age=int(input())
diff=18-age

if age>=18:
    print("if you're "+str(age)+", that means it's beer o'clock!")
if age<18:
    print('That means you have to wait '+str(diff)+' years to have a drink on me!')
Output:
C:\Users\sandeep\Downloads>py hh hello world! What is your name? user1 That is a nice name, user1 How old are you, user1 p.s. put a zero infront of a single digit 2 That means you have to wait 16 years to have a drink on me! C:\Users\sandeep\Downloads>py hh hello world! What is your name? user2 That is a nice name, user2 How old are you, user2 p.s. put a zero infront of a single digit 0 That means you have to wait 18 years to have a drink on me! C:\Users\sandeep\Downloads>py hh hello world! What is your name? user3 That is a nice name, user3 How old are you, user3 p.s. put a zero infront of a single digit 25 if you're 25, that means it's beer o'clock!
Best Regards,
Sandeep.

GANGA SANDEEP KUMAR


RE: Single digits seem to be greater than double digits - Frosty_GM - Nov-20-2020

Ahh legend thank you!

I tried playing around with str(int(age)) stuff like that but obviously didnt work.

Thanks for the help


RE: Single digits seem to be greater than double digits - perfringo - Nov-20-2020

Comparing strings doesn't work like comparing numbers.

String comparison is based on character code points and can be desribed as follows:

- comparing the n-th characters of each string (starting with 0-th index) using the == operator
- if they’re equal, repeat this step with the next character
- in case of two unequal characters, string with the character that has the lower code point is 'less' than other
- if all characters are equal, the strings are equal
- if one string is shorter i.e. runs out of characters during comparison (one string is a “prefix” of the other), the shorter string is “less than” the longer one

Codepoints for characters 0..9 are:

>>> [ord(str(i)) for i in range(10)]
[48, 49, 50, 51, 52, 53, 54, 55, 56, 57]
Therefore:

>>> '18' < '2'   # code point of '1' is smaller than of '2'
True
>>> '18' < '02'          # codepoint of '1' is larger than of '0'
False



RE: Single digits seem to be greater than double digits - DeaD_EyE - Nov-20-2020

Please use the
[python][/python]
tags to wrap your code. Otherwise the indentation is lost and syntax highlighting does not work.

print("hello world!")
print("What is your name?")
name = input()

print("That is a nice name, " + name)
print("How old are you, " + name + " p.s. put a zero infront of a single digit")
age = input()

if age >= "18":
    print("if you're " + age + ", that means it's beer o'clock!")
diff = str(18 - (int(age)))
if age < "18":
    print("That means you have to wait " + diff + " years to have a drink on me!")
In Line 9 you're comparing two strings.
The comparison of str is made via lexicographical order.

Here are some hexadecimal values:
Whitespace == 0x20
0 - 9 == 0x30 - 0x39
A - Z == 0x41 - 0x5a
a - z == 0x61 - 0x7a

If you compare this:
"1" > "18"

The interpreter compares char by char.
(0x31,) > (0x31, 0x38)
But if you compare "4" > "18", you have this values:
(0x34,) > (0x31, 0x38)
So, the first one is bigger and this explains why "4" > "18" == True.

If you want to compare numbers, then convert the str into int.



print("hello world!")
print("What is your name?")
name = input()

print("That is a nice name, " + name)
print("How old are you, " + name + " p.s. put a zero infront of a single digit")
age = int(input())

if age >= 18:
    print("if you're " + str(age) + ", that means it's beer o'clock!")
diff = 18 - age
if age < 18:
    print("That means you have to wait " + str(diff) + " years to have a drink on me!")
The concatenation of str should not be done with + operator. If you work with int, float or other types, you've to convert them back to a str. Instead, use string formatting. With modern Python, we have f-strings. A f in front of the " introduces a format-string. The names in the curly braces are replaced with their values. For example, {age} is replaced with the age, which is an int.

print("hello world!")
print("What is your name?")
name = input()

print(f"That is a nice name {name}")
print(f"How old are you {name}")
age = int(input())

if age >= 18:
    print(f"if you're {age} that means it's beer o'clock!")
diff = 18 - age
if age < 18:
    print(f"That means you have to wait {diff} years to have a drink on me!")
The benefit is, that you don't have to convert the int explicit back to a str if you're using string formatting.