Compare texts and numbers possible?


#1
a = raw_input("Number a")
b = raw_input("Number b")
def the_flying_circus():
    if (a > 3) and (a != b):    # Start coding here!
        # Don't forget to indent
        # the code inside this block!
        print "What the fuck?"
        return True
    elif (a > 2) and (a == b):
        # Keep going here.
        # You'll want to add the else statement, too!
        print "Fuck this shit"
        return True
    else:
        print "Yeah fuck you too!"
        return True
        
print a
print the_flying_circus()

So in this test, I can actually compare text to number without any problem. I input a for a and b for b variables, and they can actually be compared to numbers.

So what I got was a = 2, b = 3...

but if I try to print the variables, they are still text a,b,c...

So how does the raw_input actually does here? And what character has the 0 and 1 value?


#2

What you do here is, to assign a string to the variables a and b.

What it does is, to take your input, whatever it is, and turn it into a string. Even if you type in an integer, it will be converted into a string.So if you type a 3 and compare it to 3, it will be "3" == 3, which is False, because "3" is a string and 3 is a integer.

One way to fix it, is:

a = int(raw_input("Number a"))
or
a = input("Number a")

As far as I know, Python takes 0 as False, and 1 as True.


#3

I can actually compare the string with number. Don't believe me?

# Make sure that the_flying_circus() returns True
a = raw_input("Number a")
b = raw_input("Number b")
def the_flying_circus():
    if (a > 3) and (a != b):    # Start coding here!
        # Don't forget to indent
        # the code inside this block!
        print "What the fuck?"
        return True
    elif (a > 2) and (a == b):
        # Keep going here.
        # You'll want to add the else statement, too!
        print "Fuck this shit"
        return True
    else:
        print "Yeah fuck you too!"
        return True
        
print a
print the_flying_circus()

Result:

Number a a
Number b b
a
What the fuck?
True
None

#4

@formoney000: The thing is whatever you get from the user using raw_input(), in this case you want a number, and you don't use int() or float() functions to convert the value stored in a and/or b, you'll have two entries as strings.
If you do:

print type(a)

You'll get a message informing that the type of "a" is str.

Q: But how does it really works?
A: "When you order two strings or two numeric types the ordering is done in the expected way (lexicographic ordering for string, numeric ordering for integers).

When you order a numeric and a non-numeric type, the numeric type comes first.

>>> 5 < 'foo'
True
>>> 5 < (1, 2)
True
>>> 5 < {}
True
>>> 5 < [1, 2]
True

When you order two incompatible types where neither is numeric, they are ordered by the alphabetical order of their typenames:

>>> [1, 2] > 'foo'   # 'list' < 'str' 
False
>>> (1, 2) > 'foo'   # 'tuple' > 'str'
True

"

Source: How does Python compare string and int?

Happy coding!


#5

Welp, things make sense now. The people from codecademy should explain this one in details like you do to prevent confusion. Thanks.


#6

@formoney000: I think each person has their own way to explain things.
Some like to go on details, while others prefer to go straight to the point.

I believe it all depends on the questions you make, some don't really need further explanation while others do but I'm glad my explanation was clear and helped you out!