Home > Enterprise >  Can you assign multiple strings to the same variable on the same line?
Can you assign multiple strings to the same variable on the same line?

Time:09-22

I'm having trouble writing a program that asks for your birthday and then the user can confirm if their inputted birthday is true or false (yes or no), however, I want to be able to accept an answer of "yes" if it was spelt with an accidental capital visa Veras for "no"

For example if I were to input "Yes" instead of "yes" as an answer to whether or not the correct birthday is being displayed in the console, the computer would still accept that as an answer as the word is still spelt correctly.

So in that case, I am trying to understand if I can assign multiple ways of spelling "yes" to one variable so I don't have to type all the ways you can write the word "yes" and assign it too different variables.

Here is what I have tried for my code:

answer_YES = 'Yes' or "yes"
answer_NO = 'No' 

Name = input("What's your name? ")
Last_Name = input("What's your last name? ")
print("\nHello,", Name, Last_Name   "!")

num1 = float(input("Give me one number: "))
num2 = float(input("Give me a better number: "))

print("\nHere is your Summary:")
print(int(num1), ' ', int(num2), '=', int(num1   num2))

BD = input("\nOK, now give me your birthday m/d/y: ")
print("Is this your birthday?", '"'   BD   '"' "\n\nCorrect? (Yes) Incorrect? (No)\n")
answer = input("Answer: ")

#Using Boolean Expressions

if answer == answer_YES:
    print("Awesome! Thank you", Name)

CodePudding user response:

if it is all about the yes spelling (Yes or YEs or YES ...) you can have

anser_YES = 'yes'

then in the validation you do something like

if answer.lower() == answer_YES

otherway if you want make multilang input accept you can use list of accepted answers, so instead of

answer_YES = 'yes'

do

answer_YES = ['yes', 'oui', 'Yeeeeeessss']

then to test the true response do

if answer in answer_YES:

i wish this was helpful for you

CodePudding user response:

You can use the in keyword

answer_YES = ("Yes", "yes")
if answer in answer_YES:
    do_something()

or use

if answer.lower() == "yes":

CodePudding user response:

You are not able to assign multiple values to a variable.

If you just want a simple check, I would use the lower() function to make the strings match without worrying about capitalization. An alternative would be to search for it in a set.

answer_YES = "yes"

...

if answer.lower() == answer_YES:
    print("Awesome! Thank you", Name)

or

answer_YES = {"Yes", "yes"} 

...

if answer in answer_YES:
    print("Awesome! Thank you", Name)
  • Related