Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 How to print a statement if a user's calculated number is between two floats Bruizeh Programmer named Tim Posts: 7 Threads: 3 Joined: Feb 2019 Reputation: 0 Likes received: 0 #1 Feb-10-2019, 10:25 AM Hello, I have a problem. So I made a formula that calculates a User's BMI based on their height and weight, and gave it the variable userBmi. I would like it that if userBMI is between two floats, it prints a specific message, e.g. "You're Healthy" / "You're Overweight" ```userBmi = round(weight / (height * height), 2) break except ValueError: print("Incorrect, Try again") if userEth == "European": if userBmi < underWeight: print("Your BMI is", userBmi) print(under_msg) if userEth == "European": if userBmi < 18.5 && > 24.9: print("You're healthy")```This is just a snippet of the code I'm making, and the issue is the last 3 lines. It yields a syntax error, I don't know if I'm implementing the functions code or what, but any help you guys can give will be really appreciated. PS: UserEth stands for the user's ethnicity, and a certain ethnicity determines whether the same BMI can be classified as UnderWeight or OverWeight for different ethnicity perfringo Da Bishop Posts: 1,355 Threads: 7 Joined: Jun 2018 Reputation: 137 Likes received: 298 #2 Feb-10-2019, 10:59 AM Your problem is on row # 12. You should write: `if 18.5 < userBmi < 24.9:` It is good practice to use 'growing pattern' i.e. values what are before are smaller. It it mentally easier to parse in which range number should be. I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. DeaD_EyE Da Bishop Posts: 1,299 Threads: 5 Joined: May 2017 Reputation: 146 Likes received: 289 #3 Feb-10-2019, 12:21 PM ```if userBmi < 18.5 && > 24.9: ```You are comparing 24.9 against what? Also the && is not an operator and you should not use the bitwise operator `&`. Wrong code: `userBmi < 18.5 & userBmi > 24.9`This raises a `TypeError`. The `&` operator has a higher precedence as comparison operators. The wrong code tries a bitwise and with the values 18.5 and userBmi. Source: operator-precedence Still wrong, but no Exception `(userBmi < 18.5) & (userBmi > 24.9)`There happens a bitwise and with two boolean. Still wrong, but syntactically nicer `userBmi < 18.5 and userBmi > 24.9`This works, because the operator `and` has a lower precedence as a comparison operator. Forget the greater than sign: Don't use the greater than sign in programming But this is always `False`. Better solution in my opinion: ```if 18.5 < userBmi < 24.9: ```This means 18.5 is smaller as userBmi, userBmi is smaller as 24.9. 18.5 -> False 18.6 -> True 24.8 -> True 24.9 -> False The programmer who has written the article about the greater then sign, does not talk about python. In Python you can chain comparisons. The same solution where 24.9 and 18.5 are also True: ```if 18.5 <= userBmi <= 24.9: ```18.4 -> False 18.5 -> True 18.6 -> True 24.8 -> True 24.9 -> True 25.0 -> False The same solution where 24.9 and 18.5 are also True, but this time inverted: ```if not 18.5 <= userBmi <= 24.9: ```18.4 -> True 18.5 -> False 18.6 -> False 24.8 -> False 24.9 -> False 25.0 -> True I hope it helps. Please read the stuff. Operator precedence is inverted. Usually you get in the documentation of languages the highest operator precedence as first (descending order). In the python docs the precedence is listed from low to high (ascending order). It is very handy to chain the comparison. Last time I missed this feature in Structured Control Language? nilamo likes this post 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 How can I print the number of unique elements in a list? AnOddGirl 5 269 Mar-24-2020, 05:47 AM Last Post: AnOddGirl Print the longest str from user input edwdas 5 529 Nov-04-2019, 02:02 PM Last Post: perfringo Print the Largest Odd Number Than999 5 767 Jul-24-2019, 11:35 AM Last Post: jefsummers Round off floats in a list nagymusic 10 997 Apr-02-2019, 12:43 AM Last Post: nagymusic How to force print statement to print on one line wlsa 4 832 Oct-28-2018, 09:39 PM Last Post: wavic Print The Length Of The Longest Run in a User Input List Ashman111 3 976 Oct-26-2018, 06:56 PM Last Post: gruntfutuk Help - why is my print statement being ignored? dmhdz 2 585 Aug-06-2018, 05:23 PM Last Post: dmhdz def functions and floats alwillia 3 1,150 May-26-2018, 07:31 PM Last Post: buran Help Formatting Print Statement (input from 3 lists) X 2 Hebruiser 11 2,424 Dec-06-2017, 04:47 PM Last Post: gruntfutuk User input only takes the last number Austin11 16 3,418 Nov-28-2017, 11:20 PM Last Post: Prrz

Forum Jump:

Users browsing this thread: 1 Guest(s)