I am trying to loop through all the possible outcomes of the variable if_yes so that I can calculate the BMI
Here is the code:
Question = input("Do you want to calculate your BMI?")
if_yes = ["Yes", "yes", "y", "Y"]
for i in if_yes:
if Question == i:
bmi_calculation()
elif Question == "No" or Question == "N" or Question == "no" or Question == "n":
print("Thank you.Hope you'll use this program in the future")
else:
print("Enter a valid response!")
Whenever I write Y the else statement seems to execute 3 times and then the bmi_calculation() function seems to execute. How can I avoid that?
Output
Do you want to calculate your BMI?Y
Enter a valid response!
Enter a valid response!
Enter a valid response!
Please Enter your Name:
7 Answers 7
The reason why it's showing "Enter a valid response!" 3 times before executing the program is because when it is iterating through the list, "Y" (which the user entered) does not satisfy the condition that "Y" is equal to i (which takes the first 3 values from the list). What you can try is by using membership operators instead of iterating through the list like:
Question = input("Do you want to calculate your BMI? ")
if_yes = ["Yes", "yes", "y", "Y"]
if Question in if_yes:
bmi_calculation()
elif Question in ["no", "No", "n", "N"]:
print("Thank you.Hope you'll use this program in the future")
else:
print("Enter a valid response!")
1 Comment
The reason it's doing this is because the first three times it's comparing your "Y" against "Yes", "yes", and "y", which are all not what the answer was. If you're set on keeping this way of coding how you want to do this, instead try factoring your code like this (psuedocode, I'm not a python programmer)
Question = input("Do you want to calculate your BMI?")
if_yes = ["Yes", "yes", "y", "Y"]
yes_value = false
for i in if_yes:
if Question == i:
yes_value = true
if yes_value == true
bmicalculation()
1 Comment
The reason it is running multiple times is because you are looping through if_yes which has multiple values. I think what you want to do is the following:
Question = None
if_yes = ["Yes", "yes", "y", "Y"]
while Question not in if_yes:
Question = input("Do you want to calculate your BMI?")
if Question == "No" or Question == "N" or Question == "no" or Question == "n":
print("Thank you.Hope you'll use this program in the future")
exit()
else:
print("Enter a valid response!")
bmi_calculation()
This will basically force a user to either input something that is in is_yes or some type of no to exit the program.
Comments
You are not using properly the for loop, in the end what you need to do is to compute or not compute the BMI based on the input that the user provides:
question = input("Do you want to calculate your BMI?")
if_yes = ["Yes", "yes", "y", "Y"]
if question in if_yes:
bmi_calculation()
elif question in ["No", "N", "no", "n"]:
print("Thank you.Hope you'll use this program in the future")
else:
print("Enter a valid response!")
I have also added some improvements to your logic. Follow, PEP-8 to style your code.
1 Comment
You're doing it the wrong way, try using a while loop so it keeps repeating until the correct input is given, instead of looping through the right inputs
question = input("Do you want to calculate your BMI?\n1. Yes\n2. No\n")
while True:
if question == "1":
bmi_calculation()
break
elif question == "2":
print("Hope you use the program in the future")
break
else:
question = input("Please enter a valid response\n")
1 Comment
This will work:
import time
def bmi_calculation():
height = float(input("What is your height (metres?) "))
weight = float(input("What is your weight (kg?) "))
print("Your BMI is ", weight / height**2)
answer = input("Do you want to calculate your BMI? ")
yes_answers = ["Yes", "yes", "y", "Y"]
no_answers = ["No", "N", "no", "n"]
if answer in yes_answers:
bmi_calculation()
elif answer in no_answers:
print("What are you afraid of?")
while True:
try:
time.sleep(1)
print("scaredy-cat")
except KeyboardInterrupt:
pass
else:
print("Enter a valid response!")
Comments
I would do the following
yes_answer = ['y', 'Y', 'Yes', 'yes', 'YES']
no_answer = ['n', 'N', 'No', 'no', 'NO']
valid_answer = yes_answer + no_answer
answer = ''
while answer not in valid_answer:
answer = input("Do you want to calculate your BMI? ")
if answer in yes_answer:
print('We will calculate your BMI')
elif answer in no_answer:
print("Thank you.Hope you'll use this program in the future")
else:
print("Enter a valid response!")
if_yesfor a single user input. You want to check for membership of the input in your listif Question in if_yes:...actually you handle to no part for Question correctly except for consistency you should haveif_no ['n', 'N', 'No', 'no']