If Statements from Strings


#1

Hey everyone,

I am working on an exercise that I can't seem to figure out. I'm working in Python 3.5. I am trying to collect 2 inputs from a user, add them together and then print out a response based on the sum of the 2 inputs. I got stuck a few times and found the solutions however now I am really confused. Can anyone please guide me toward what may help fix my code?

I get the following error message:

Traceback (most recent call last):
File ..... line 7, in
if Sum > "100":
TypeError: unorderable types: int() > str()
When running:

print("I want you to think of 2 numbers. Got em? Okay")
print("Please Enter Your First Number")
FirstNumber = input()
print("Perfect. Now please enter the second number.")
SecondNumber = input()
Sum = int(FirstNumber) + int(SecondNumber)
if Sum > "100":
    print("They add up to a big number")
if Sum <= "100":
    print(Sum)

Thank you for any and all help!!


#2

Python does not permit comparison of a number to a string. Sum is a number, "100" is a string.

Naming convention dictates that we use lowercase as the first letter in plain variables. A capital first letter indicates a Class associated with the name.

firstNumber or first_number

secondNumber or second_number

sum

It's also a good practice to not use reserved words as variable names. sum() is a built in list function.


#3

Hi mtf, thank you for your response!

I had thought the issue was comparing the variable to a number so I tried wrapping the Sum in int() but hadn't thought of just removing the quotation marks from 100.. And sure enough that was the answer. Thanks for your help!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.