Python Forum
Single digits seem to be greater than double digits
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Single digits seem to be greater than double digits
#5
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Single digits seem to be greater than double digits - by DeaD_EyE - Nov-20-2020, 10:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  If a set element has digits in the element tester_V 3 442 Mar-25-2024, 04:43 PM
Last Post: deanhystad
  method to remove zero digits from fraction in decimal Skaperen 17 3,147 Oct-23-2022, 04:02 AM
Last Post: Skaperen
  First line with digits before last line tester_V 5 1,569 Aug-22-2022, 09:04 PM
Last Post: tester_V
  Finding First Digits in String giddyhead 4 1,482 Aug-17-2022, 08:12 PM
Last Post: giddyhead
  Adding Decimals to classes with OOP + rounding to significant digits (ATM demo) Drone4four 7 2,485 May-04-2022, 06:15 AM
Last Post: Drone4four
  checking for valid hexadecimal digits Skaperen 3 6,690 Sep-02-2021, 07:22 AM
Last Post: buran
Photo How can I use 2 digits format for all the number? plumberpy 6 2,494 Aug-09-2021, 02:16 PM
Last Post: plumberpy
  Remove single and double quotes from a csv file in 3 to 4 column shantanu97 0 7,153 Mar-31-2021, 10:52 AM
Last Post: shantanu97
  Digits of a number 1234 2 1,921 Nov-27-2020, 05:43 PM
Last Post: perfringo
  Printing digits after the decimal point one by one uberman321 1 1,836 Oct-20-2020, 08:10 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020