Python Forum
Check if integer is between two values - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Check if integer is between two values (/thread-7156.html)



Check if integer is between two values - Wolfpack2605 - Dec-24-2017

Hi,
I am quite new to Python and I am having trouble with solving a problem with an online course I am undertaking on Grok Learning. This is the question
Write a program that checks how long a name is. The program should take a name as input from the user.

If the name has 3 or fewer letters, your program should work like this:


Enter your name: Lin
Hi Lin, you have a short name.

If the name has between 4 and 8 letters (inclusive), your program should work like this:


Enter your name: Jimmy
Hi Jimmy, nice to meet you.

Otherwise, if the name has more than 8 letters, your program should work like this:


Enter your name: Yaasmeena
Hi Yaasmeena, you have a long name.

This is what I have done:
name=input("Enter your name: ")
x=(len(name))
if x<=3:
print("Hi", name+",", "you have a short name.")
if x>=8:
print("Hi", name+",", "you have a long name.")
else:
print("Hi",name+",","nice to meet you.")

The problem I am having is when a name with a length of 3 or less is entered, the out put is:
Enter your name: JP
Hi JP, you have a short name.
Hi JP, nice to meet you.


when it was meant to output:

Enter your name: JP
Hi JP, you have a short name.

I need a way to differentiate between the lengths.
Please help. thanks


RE: Check if integer is between two values - j.crater - Dec-24-2017

Hello, please edit your post so that your code is included in the Python code tags. Also, when copying the code, use ctrl+shift+v to preserve indentation. You can find help here.


RE: Check if integer is between two values - mpd - Dec-24-2017

Your problem is that the two if-statements are independent of each other. You're basically saying "if x <= 3 do something" then you say "if x >=8 then do another thing, otherwise do some third thing." It's subtle but you should notice that I intentionally grouped your if statements into two independent questions.

Take a look at this: https://docs.python.org/3/tutorial/controlflow.html#if-statements

Also, please put your code inside python tags so it gets nicely formatted.
this:
if x >= 3:
    print("x is less than three")
instead of:
if x >= 3:
print("x is less than three")


RE: Check if integer is between two values - DeaD_EyE - Dec-24-2017

name = 'Chuck Norris'
if 4 <= len(name) <= 8:
    print('Name "{}" is between 4 and 8 chars long'.format(name))
else:
    print('Name "{}" is {} chars long and does not fit.'.format(name, len(name)))

name = 'Chuck'
if 4 <= len(name) <= 8:
    print('Name "{}" is between 4 and 8 chars long'.format(name))
else:
    print('Name "{}" is {} chars long and does not fit.'.format(name, len(name)))
You can do multiple compares. Just read it as: "4 is smaller or equal then name. Name is smaller or equal then 8.