Can you write "while loops condition under another while loops condition? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can you write "while loops condition under another while loops condition?

while BMI <18.5: print("under") while (BMI>=18.5) and (BMI<25): print("normal")

5th Mar 2022, 6:27 PM
Clinton
15 Answers
5th Mar 2022, 11:42 PM
HungryTradie
HungryTradie - avatar
+ 5
You can do it but in you code second while loop won't run, it will rise error becouse loop will run forever and it should look like this(I didn't included stopping the loop): while BMI <18.5: print("under") while (BMI>=18.5) and (BMI<25): print("normal") But for code I suppose you want to do I would propose using if/elif/else statment like this if BMI<18.5: print("under") elif BMI<25: print("normal") else: print("BMI is more than 25")
5th Mar 2022, 7:28 PM
Bartek
+ 3
But it seems like you are trying to use a while loop as a if statement. And you are not updating BMI. This is an infinite loop.
5th Mar 2022, 7:28 PM
Mustafa A
Mustafa A - avatar
+ 3
height=float(input()) weight=float(input()) BMI=weight/height**2 if BMI <18.5: print("underweight") elif (BMI >=18.5) and (BMI <25): print("normal") elif(BMI >=25) and (BMI <30): print("overweight") else: print("obesity")
5th Mar 2022, 8:37 PM
Clinton
+ 3
Ok I will still try with the if/elif/else statements.i tried at first but didn't work that's why I asked if it would work if I use the while loops
6th Mar 2022, 9:40 PM
Clinton
+ 2
Of course you can have a while loop inside a while loop.
5th Mar 2022, 7:26 PM
Mustafa A
Mustafa A - avatar
+ 2
Yes, you can write while under while loop , but when there if else is available in python then why you are choosing critical condition. Use if else , nested if.
6th Mar 2022, 7:33 AM
Mansi Srivastava
Mansi Srivastava - avatar
+ 2
Yes it's possible but will have to include a condition to break the loop , if not it is an Infinity loop which will result to an error
7th Mar 2022, 1:00 PM
Abdallah Umar Jibir
Abdallah Umar Jibir - avatar
+ 1
Use if elif else
5th Mar 2022, 7:41 PM
Shadoff
Shadoff - avatar
0
anything wrong with the code
5th Mar 2022, 8:37 PM
Clinton
0
I just did scroll up to see it now
5th Mar 2022, 9:29 PM
Clinton
0
Yes why not. It's possible
6th Mar 2022, 5:17 PM
Shoaib Farooq
Shoaib Farooq - avatar
0
Personally, I think 👎 while (condition and condition): #your code break is the wrong thing to use for this task. I also didn't check the task to see that the logic you were trying to use was correct. It may have an >= incorrectly used where a > was required ..... I solved it (as did most others) with 👍 if elif else. {EDIT: I did check the task, my code passed all tests} {EDIT2: Check your output for correct spelling capitalisation and punctuation. It must match the expected output exactly}
6th Mar 2022, 9:21 PM
HungryTradie
HungryTradie - avatar
0
Yes you cN
7th Mar 2022, 9:19 AM
Renz Jefferson Padro
Renz Jefferson Padro - avatar
- 1
Clinton Obeng Your condition is not changing. You will end up with an infinite loop. Just use if statements as suggested by others.
5th Mar 2022, 9:27 PM
Mustafa A
Mustafa A - avatar