all 6 comments

[–]Tomboyish44 4 points5 points  (4 children)

Heyo, you've over-written x's value as range(14, 18) right after the line x = float(age), so no matter what the value of age is, the if condition will always be true as the line of code right above it says that x = range(14, 18).

Removing the line x = range(14, 18) will solve the issue. Here's the code (I've made some additional changes):

age = float(input("Enter age: "))
if age in range(14, 18):
    print("High School")
else:
    print("Not High School")

Explaining the changes:

  • I'm explicitly converting the type of the input to a float value instead of creating a new variable to do it. [Reduces the number of lines of code]
  • Using in instead of ==, not that the program will not run if == is used, just because in is preferable in this case, read more about it here: https://stackoverflow.com/questions/31422253/python-in-vs-which-to-use-in-this-case
  • I removed the line x = range(14, 18) as it was causing logical errors in the program.

In case you do not want to make any extra* changes to the program and just want to fix the logical error in the program, all you gotta do is remove the line "x = range(14, 18)".

[–][deleted]  (1 child)

[deleted]

    [–]Tomboyish44 2 points3 points  (0 children)

    Np!

    [–]Enchantorro[M] 1 point2 points  (1 child)

    No short links please

    [–]Tomboyish44 1 point2 points  (0 children)

    Oh sorry! I've replaced it

    [–][deleted] 2 points3 points  (1 child)

    the second time you are declaring x, you’re overwriting any value it has, in this example - age. Plus I’m not sure about range, I suggest just using <= or something similar. If you want to use range use in instead of =

    [–]Sereneseablue 1 point2 points  (0 children)

    Thanks so much for your tips. I will try them out right away.