Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Check if integer is between two values Wolfpack2605 Programmer named Tim Posts: 11 Threads: 10 Joined: Dec 2017 Reputation: 0 Likes received: 0 #1 Dec-24-2017, 12:47 AM 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 j.crater Posts: 1,069 Threads: 33 Joined: Sep 2016 Reputation: 41 Likes received: 150 #2 Dec-24-2017, 01:30 AM 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. mpd Lumberjack Posts: 100 Threads: 3 Joined: Dec 2017 Reputation: 3 Likes received: 17 #3 Dec-24-2017, 01:33 AM 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") DeaD_EyE Da Bishop Posts: 1,109 Threads: 5 Joined: May 2017 Reputation: 109 Likes received: 244 #4 Dec-24-2017, 06:39 AM ```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! « Next Oldest | Next Newest »

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)