0

I am a beginner when it comes to coding. I am trying to run a program on python that takes in kilometers(user input) and returns it in miles(output). I know how to do the task but I wanted to challenge myself by using if statements to check if the user has entered a number:

Kilometers = input("Please insert number of Kilometers: ")
if type(Kilometers) == int or float:
    print("This is equivalent to", float(Kilometers)/1.609344, "Mile(s)")
else:
    print("This is not a number")

I understand that whatever the user inputs will be saved as a string. However, whenever I run the code, the program always tries to convert the input into miles.

I have specified in the if statement that the type has to equal a float or an int, so shouldn't the output always be "This is not a number" until I change the first line to:

Kilometers = float(input("Please insert number of Kilometers: "))

or

Kilometers = int(input("Please insert number of Kilometers: "))
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
0Gravity
  • 3
  • 2
  • You might prefer to use `isinstance`, as described here: [Python check if isinstance any type in list?](/a/33311330/4518341) That'll allow for subclasses. Or you might prefer to [ask the user for input until they give a valid response](/q/23294658/4518341). – wjandrea Feb 01 '22 at 22:59
  • I'll give you a hint: bool(float)) -> True – Antonio Leonti Feb 01 '22 at 23:00
  • `if type(Kilometers) == int or float` is not doing what you think. – John Gordon Feb 01 '22 at 23:02
  • Welcome to Stack Overflow. There are multiple issues with this code; please read each linked duplicate carefully. Aside from the other comments: `input` will give you *a string, no matter what*. You seem to understand this: "I understand that whatever the user inputs will be saved as a string." Therefore, you should expect `type(Kilometers)` to be `str` - yes? – Karl Knechtel Feb 01 '22 at 23:03
  • @Karl Yeah, OP's aware that `type(Kilometers)` will be `str`, so they're expecting the `else` to always run. But the `or` is not doing what they expect. – wjandrea Feb 01 '22 at 23:08

1 Answers1

1

When programming if statements in Python, each condition must be fully rewritten. For example, you would write if type(Kilometers) == int or type(Kilometers) == float rather than if type(Kilometers) == int or float. Another important thing in your code is that if someone inputs 5.5, you would expect a float value, but Python interprets that to be a string. In order to circumvent this, you can use a try/except clause like this:

Kilometers = input("Please insert number of Kilometers: ")
try:
    Kilometers = float(Kilometers)
    print("This is equivalent to", Kilometers/1.609344, "Mile(s)")
except ValueError:
    print("This is not a number")

What this is doing is trying to set Kilometers as a float type, and if the user inputs a string that cannot be interpreted as a float, it will cause a ValueError, and then the code inside except will run. You can find more on try/except clauses here: Python Try Except - W3Schools

One more thing I noticed is that you should name your variables according to the standard naming conventions, so Kilometers should be kilometers. You can find more on the naming conventions here: PEP 8

Timmy Diehl
  • 409
  • 3
  • 16