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
  sending arduino data to python and use a parity check 10_Lucas 6 264 Aug-03-2019, 10:37 AM
Last Post: DeaD_EyE
  Converting String to Integer Python Johnny1998 5 134 Aug-02-2019, 08:13 PM
Last Post: Johnny1998
  integer representing a 10-digit phone number critter70 5 672 May-17-2019, 09:29 AM
Last Post: saravanatn
  integer +-* interval mcgrim 2 218 May-08-2019, 11:53 AM
Last Post: mcgrim
  Please check code Evgeniy2019 4 281 Apr-05-2019, 06:37 PM
Last Post: Evgeniy2019
  How to check an array exist in a file using Python fitrisibarani 17 760 Feb-27-2019, 04:16 PM
Last Post: ichabod801
  Hi how to take row 1 for every column and check the value what class is it kirito85 2 482 Nov-21-2018, 06:52 AM
Last Post: kirito85
  Integer object error Zatoichi 2 1,023 Feb-18-2018, 07:25 AM
Last Post: Zatoichi
  Writing a function that accepts two integer parameters (lines and cheers) taydeal20 1 763 Feb-05-2018, 08:35 PM
Last Post: nilamo
  Check if string is uppercase or lowercase and eliminate Wolfpack2605 1 1,536 Jan-01-2018, 05:03 AM
Last Post: Mekire

Forum Jump:


Users browsing this thread: 1 Guest(s)