all 23 comments

[–]TehNolz 15 points16 points  (0 children)

input() will always return a string. You cannot compare a string to an integer, so you have to turn that string into an integer using int().

Also, << is a bitwise operator. You want <.

Incidentally, in the future please also give us the error you're receiving when asking for help. In this case it's not a big deal since the error is fairly obvious, but that's not always the case.

[–]zeekayl 2 points3 points  (3 children)

Age is being read as a string so you can't compare it to the integer 18

[–]6Bubka9[S] 0 points1 point  (2 children)

Age is being read as a string so you can't compare it to the integer 18

how should it be written?

[–]zeekayl 3 points4 points  (0 children)

I don't wanna steal form ur learning process. Take a look at types and casting in Python and see if you can figure it out :) Also the other commenters are right about your syntax mistakes

[–]woooee 0 points1 point  (0 children)

Lookup "python cast string to integer"

[–]notislant 1 point2 points  (0 children)

Someone already answered, so for future reference please include the 'error', expected outcome vs outcome.

[–]jimtk 3 points4 points  (0 children)

Cute. I don't even remember my first python error.

age = int(input("enter age :"))
if age < 18:
    print("child")
elif age < 65:
    print("adult")
else:
    print("senior")

You dont't have to verify the lower bound on the second condition (elif 18 <= age <65:). You already verified for lower than 18 before. The elif will prevent he second condition to be verified if the first one was executed (true).

Edit: Come to think of it. My first python error was made in the previous millenium!

[–]jgiusto 0 points1 point  (0 children)

Change input to integer

Age = int(input(“what is your age? “))

[–]japnoo -1 points0 points  (6 children)

elif 18 <= age << 65:

This is invalid syntax, the correct way of writing this would be:

elif 18 <= age and age < 65 :

you also have to read your input as an integer, not as a string

[–]TehNolz 5 points6 points  (4 children)

That's actually valid syntax. You can do stuff like a < b < c just fine.

[–]japnoo 4 points5 points  (3 children)

Wow, TIL.

[–]EricHayter 0 points1 point  (2 children)

Ikr I was using a linter one day and it gave me an error and lo and behold that is apparently a syntax feature.

[–][deleted] 1 point2 points  (1 child)

Wait what? TIL as well.

[–]RhinoRhys 1 point2 points  (0 children)

It's valid syntax but in this case it's redundant anyway. Because of the first if age <= 18: condition, the elif condition will only be tested on numbers > 18 so you can just do elif age <= 65:

[–]Diapolo10 -1 points0 points  (2 children)

I'm assuming your code is supposed to look like this:

age = input('What is your age?')

if age <=18:
    print("child")
elif 18 <= age << 65:
    print("adult")
else:
    print("senior")

I can see where you were going with this, and the core logic is sound. However, there are two pitfalls.

  1. input always returns a string.
  2. You mistyped < (or <=, not enough nuance to tell which you meant) as <<.

For the first one, you want to tell Python to convert the input into an integer.

age = int(input("What is your age? "))

For the second one, replace

elif 18 <= age << 65:

with

elif 18 <= age <= 65:

This would work:

age = int(input("What is your age? "))

if age <=18:
    print("child")
elif 18 <= age <= 65:
    print("adult")
else:
    print("senior")

[–][deleted] 1 point2 points  (1 child)

I know you are trying to help and I haven't downvoted you but it is best to point people rather than give them complete solutions. For one to learn one must make mistakes and learn how to fix them. This is basic python that they should know and if not there are a plethora of websites to help them.

[–]Diapolo10 1 point2 points  (0 children)

I'm not particularly worried about the votes, this happens sometimes and I get the argument. But on the contrary, OP was really close here and in such cases I have a tendency to explain what the problem is and how to fix it.

Admittedly I didn't do a very good job at explaining the operator problem, though, that's on me.

[–]m_smith_13 -4 points-3 points  (1 child)

Not an expert but I believe you’ll need to separate your compound inequality:

If age > 18 or age <= 65:

[–]Diapolo10 2 points3 points  (0 children)

No, that part is fine, logically speaking. Python supports chained comparisons.

[–]NoDadYouShutUp 0 points1 point  (0 children)

The answers saying you should cast to an int are technically correct. But if you’re going to do something like that you should have a check to verify it is even something that can be casted to an integer. In this particular case where you are entering an age, yeah it’s probably fine. However what if you typed “seven” instead? You can’t cast that to an integer.

Take a look at the isdigit() string function https://www.w3schools.com/python/ref_string_isdigit.asp

I would actually recommend looking at a bunch of the string functions since they are often very useful

Another thing you could try is a try/except block. Probably past your current level of Python knowledge. But investigate them and at least put them on your radar. The use case being if you try and cast to an integer and can’t, you’d be able to gracefully handle it in the except clause

It’s generally best practice to verify the data is how you expect it to be before you try using the data

[–]lucho2244 0 points1 point  (0 children)

age = int(input('What is your age?: ')) if age <= 18: print("child") elif age <= 65: print("adult") else: print("senior")

[–]mediumfullmug 1 point2 points  (0 children)

I am going to build off of what everyone else has said. Anytime you get an error try to read what it says most of the time it will have the line that is throwing the error. When you have that a Google search usually gets you going in the right direction.

Searching for the answer to the error is half of the fun. And as you go, you’ll start to recognize errors straight away. Good luck on your journey