In Python, I am trying to figure out how to only accept a weight that is less th
ID: 2247768 • Letter: I
Question
In Python, I am trying to figure out how to only accept a weight that is less than the current weight and then reask(loop back to) the question regarding ideal weight until user gives a response that is below the current weight. How do I do this? Attached is image and I copy and pasted the code.
from operator import eq
from operator import ge
from operator import le
#asking user input in loop it will continue until correct value
while True:
overallGoal= str(input('Do you desire to lose weight or gain muscle? Please type in lose weight or gain muscle.'))
if eq(overallGoal.lower(), "lose weight"):
while True:#loop for entering int values
try:
currentWeight = int(input("Please enter current weight in pounds. "))
idealWeight = int(input("Please enter the weight you wish to obtain ultimately. "))
if ge(idealWeight, currentWeight):
print("The goal weight must be less than current weight. Please reenter information.")
break
except ValueError:#if invalid input will be given it will ask again
print("invalid entry. Please enter numeric value only.")
break#breaking loop on valid entry
from operator import eq from operator import ge from operator import le #asking user input in loop it will continue until correct value while True: overallGoal- str(input( Do you desire to lose weight or gain muscle? Please type in lose weight or gain muscle. if eq(overallGoal. lower(), "lose weight"): while True#loop for entering int values try: currentieight = int(input("Please enter current weight in pounds. ")) idealWeight = int(input("Please enter the weight you wish to obtain ultimately. ")) if ge(idealWeight, currentweight): print( The goal weight must be less than current weight. Please reenter infornation. break except ValueError#if invalid input will be given it will ask again print("invalid entry. Please enter numeric value only.") break#breaking loop on valid entryExplanation / Answer
There is a small bug in your code. You should break the loop only if input weight is less than the current weight so the break statement in you code should come in else. I'm pasting the correct code below.from operator import eq
from operator import ge
from operator import le
#asking user input in loop it will continue until correct value
while True:
overallGoal= str(input('Do you desire to lose weight or gain muscle? Please type in lose weight or gain muscle.'))
if eq(overallGoal.lower(), "lose weight"):
while True:#loop for entering int values
try:
currentWeight = int(input("Please enter current weight in pounds. "))
idealWeight = int(input("Please enter the weight you wish to obtain ultimately. "))
if ge(idealWeight, currentWeight):
print("The goal weight must be less than current weight. Please reenter information.")
else:
break # The goal weight is less than current exit from loop
except ValueError:#if invalid input will be given it will ask again
print("invalid entry. Please enter numeric value only.")
break#breaking loop on valid entry
The sample output for above program is:
Do you desire to lose weight or gain muscle? Please type in lose weight or gain muscle.lose weight
Please enter current weight in pounds. 100
Please enter the weight you wish to obtain ultimately. 101
The goal weight must be less than current weight. Please reenter information.
Please enter current weight in pounds. 102
Please enter the weight you wish to obtain ultimately. 105
The goal weight must be less than current weight. Please reenter information.
Please enter current weight in pounds. 108
Please enter the weight you wish to obtain ultimately. 107
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.