0

When I attempt to use an if statement to check if one variable is larger than another variable the if statement only checks the first digit. For example 3>10 but 30>1. How do I fix this so that the entire variable is checked?

I have tried manually setting variables and restarting my Pycharm

Alpha = input("Input first Value:")
Bravo = input("Input second Value:")
Echo = 0
if Alpha >= Bravo:
    Echo += 1
print(Echo)

When Alpha is 10 and Bravo is 3 Echo should equal 1 but instead it equals 0

Max B.
  • 1
  • 1
    `input` returns a `str` value; the values are being compared lexicographically. – chepner Feb 17 '19 at 01:15
  • See this question for reading input as numbers https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers – esorton Feb 17 '19 at 01:18

2 Answers2

1

You are comparing strings, while you need numeric values. Cast them using int() :

Alpha = int(input("Input first Value:")) 
Bravo = int(input("Input second Value:")) 
.... 
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

you're comparing strings, not numbers:

Alpha = int(input("Input first Value:"))
Bravo = int(input("Input second Value:"))
Echo = 0
if Alpha >= Bravo:
    Echo += 1
print(Echo)

It may be the Python version that's messing you up. This code works in Python 2, where an eval() is done on the entered text, but not in Python 3. Python 2's input() will return an int for numeric input, and will return an error for string input. Python 3 will just return the entered string. (I could never understand the rational behind Python 2's behavior in this regard)