Python - using more than one word in !=
how to check multiple values in if condition in python
test variable against multiple values python
variables, expressions and statements in python
python if multiple values in list
python in operator multiple values
I want to use more than one word in !=, but I can't for the life of me understand or google my way how to. Can anyone help, and explain why I can't just do it like: != "No" != "no" !="n" !="N": etc etc
answers = ["Yes", "Not now not ever.", "Unclear answer, try again.", "Maybe.", "Probably not.", "Try again later.", "My sources says no.", "My sources says yes.", "Only when there is a full moon.", "It is certain.", "Cannot predict now.", "Outlook not so good", "Very doubtful.", "You may rely on it.", "Yes - definitely.", "As I see it, yes.", "Signs point to yes."] while str(input("Do you want to ask the Magic 8Ball a question? Yes or No? ")) != "No" != "no": # <-- problematic line str(input("Ask the mighty 8Ball your question: ")) randomanswer = answers[random.randint(0, len(answers) -1)] print("\n","The Magic 8Ball says: ") print(randomanswer, "\n") else: return
You can use a tuple of sentinel-values, e.g.
while foo not in ('No', 'no', 'N', 'n'): # code
while foo.lower() not in ('no', 'n'): # code
If there are a huge amount of sentinels, a
set will perform faster than a tuple (for low amounts of values, hashing is not worth it).
You might also want to look into the
re module for more sophisticated pattern matching.
Determine whether there's more than one word in a string, Try this: line = 'how are you?' if len(line.split()) > 1: # has more than 1 word. 3. An Informal Introduction to Python¶. In the following examples, input and output are distinguished by the presence or absence of prompts (>>> and …): to repeat the example, you must type everything after the prompt, when the prompt appears; lines that do not begin with a prompt are output from the interpreter.
while str(input("Do you want to ask the Magic 8Ball a question? Yes or No? ")) not in ["No", "no"]
Stop Using “or” to Test Multiple Values Against a Variable in Python, If you've ever needed to check that a variable is equal to one of many possible values, you've probably written something like this: The keyword Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Python - using more than one word in !=
You're looking for a way to chain together conditional statements. Unfortunately you cannot do it the way you're trying to do. However, you can use
or to chain them together, which is how it's normally done. For example:
userinput = str(input("Do you want to ask the Magic 8Ball a question? Yes or No? ")) while userinput != "No" and userinput != "no": # have to get user input again at end of loop
But an even better approach is to find how to logically turn this into a single conditional, in this case using
lower on the user input:
userinput = str(input("Do you want to ask the Magic 8Ball a question? Yes or No? ")) userinput = userinput.lower() # make it lowercase while userinput != "no": # have to get user input again at end of loop
Finally, you can use
random.choice to provide a random element from your answers array.
Everything You Need to Ace Computer Science and Coding in One Big , Variables can contain letters, numbers, and some characters like an underscore. When you want to use more than one word to name avariable, you can use Python uses the word “return” to produce a value of an expression or function. At some point, you may need to break a large string down into smaller chunks, or strings. This is the opposite of concatenation which merges or combines strings into one. To do this, you use the split function. What it does is split or breakup a string and add the data to a string array using a defined separator.
Change your problematic line to:
while input("Do you want to ask the Magic 8Ball a question? Yes or No? ").strip().lower() != "no":
This way, you convert the input string to lowercase (
lower()) while also stripping any space the user might have typed in (
Then, if user types: "No",
lower() will convert it to all lowercase, "no" and then check it towards your
Anything typed into
input() is a string, so can omit the
Sams Teach Yourself Python in 24 Hours, Often, you have to use more than one word in a variable name. Separate them using an underscore rather than running them all together. numberoftoppings is With Python, you can assign one single value to several variables at the same time. This lets you initialize several variables at once, which you can reassign later in the program yourself, or through user input. Through multiple assignment, you can set the variables x, y, and z to the value of the integer 0: x = y = z = 0 print(x) print(y) print(z)
Python in 24 Hours, Sams Teach Yourself, Often, you have to use more than one word in a variable name. Separate them using an underscore rather than running them all together. numberoftoppings is What if more than one match is found? In this example, the following string is created: src_str = “This is a test sentence with titlecase. This is a test sentence with titlecase. This is a test sentence with titlecase.” By using the Python replace method, I will change “This” to “that” without specifying the count parameter.
2. Variables, expressions and statements, One of the most powerful features of a programming language is the ability to used in names with multiple words, such as my_name or price_of_tea_in_china. When the Python shell displays the value of an expression, it uses the same 7.2.1. Regular Expression Syntax¶. A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression (or if a given regular expression matches a particular string, which comes down to the same thing).
3. An Informal Introduction to Python, With Python, it is possible to use the ** operator to calculate powers 1: >>> >>> 5 ** 2 # 5 squared 25 String literals can span multiple lines. One way is using One of the things to keep in mind though is that some words have "e" in them more than once, but they should only be counted as having "e" in them once. This program runs through the word list line by line and tests to see whether "e" is in the word. If it is it keeps checking through the word to see if there are anymore "e".
- The language just isn't designed this way. Assign the input to a variable and compare this variable then to one after another.
- Maybe you could use
not ininstead? e.g.
while answer not in ['no', 'No']