1

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: 
lmiguelvargasf
71.3k55 gold badges231 silver badges241 bronze badges
asked Jul 8, 2020 at 17:13
5
  • You've written it backwards. You don't want to be iterating through if_yes for a single user input. You want to check for membership of the input in your list Commented Jul 8, 2020 at 17:16
  • 1
    You should do: if Question in if_yes: ...actually you handle to no part for Question correctly except for consistency you should have if_no ['n', 'N', 'No', 'no'] Commented Jul 8, 2020 at 17:17
  • Oh okay. Let me try Commented Jul 8, 2020 at 17:18
  • If Question.lower() in ['y', 'yes']: Commented Jul 8, 2020 at 17:18
  • @roganjosh Thanks it worked. Cheers! Commented Jul 8, 2020 at 17:19

7 Answers 7

1

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!")
Red
27.7k8 gold badges44 silver badges63 bronze badges
answered Jul 8, 2020 at 17:26
Sign up to request clarification or add additional context in comments.

1 Comment

why is this answer upvoted since it is not even syntactically correct!
1

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()
answered Jul 8, 2020 at 17:17

1 Comment

Thanks for informing that and giving me another way to solve it.. Cheers!
1

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.

answered Jul 8, 2020 at 17:25

Comments

1

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.

answered Jul 8, 2020 at 17:26

1 Comment

Thank you for the information.. I'll look into PEP-8 ASAP.
1

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")
answered Jul 8, 2020 at 17:28

1 Comment

That's a unique way to code. Never thought of that.. Thanks for that :)
1

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!")
answered Jul 8, 2020 at 17:28

Comments

0

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!")
answered Jul 8, 2020 at 17:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.