Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Check if integer is between two values
#1
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
Quote
#2
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.
Quote
#3
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/contr...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")
Quote
#4
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.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Iterating over pandas.df to check for values out of range Padowan 14 5,400 Nov-26-2017, 04:37 PM
Last Post: heiner55

Forum Jump:


Users browsing this thread: 1 Guest(s)