Python: if not true function is not running as it should [duplicate] - python

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 2 years ago.
If the user doesn't type in multiplication, addition, ect, then a string should be printed. However, if one of these words is typed it still prints this string. I've just started with python; sorry that it's such a basic question!
print ("Multiplication, Addition, Subtraction or Division?")
type = str(input("Your choice:"))
if type != "multiplication" or "addition" or "subtraction" or "division":
print("YOU DID NOT ENTER ONE OF THE ABOVE OPTIONS.\nTRY AGAIN!")
num1 = int(input("Enter your 1st number:"))

print ("Multiplication, Addition, Subtraction or Division?")
choice = input("Your choice:")
if choice.lower() not in ("multiplication", "addition", "subtraction", "division"):
print("YOU DID NOT ENTER ONE OF THE ABOVE OPTIONS.\nTRY AGAIN!")
num1 = int(input("Enter your 1st number:"))
Also type is the built-in global name. By using this name you hide it.

The problem is on your if statement. When you are testing if a value is one of multiple options, you can use the in keyword like "test" in ["hello", "test", "123"] == true.
print ("Multiplication, Addition, Subtraction or Division?")
type = str(input("Your choice:"))
if type not in ["multiplication", "addition", "subtraction", "division"]:
print("YOU DID NOT ENTER ONE OF THE ABOVE OPTIONS.")
print("TRY AGAIN!")

Related

Is it possible to make a if statement look at 2 strings independently? [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 4 months ago.
I don't know how exactly to word it so ill just explain,
elif careful != "yes" "no":
How do i make the "yes" and "no" independent? When i put it in, it combines the "yes" and "no" together.
Output 👇
'yesno'
I have tried using the '|' operator and the '&' operator
Here is the full code 👇
careful = input("Are you Careful?")
if careful == "yes":
print("ok good, what is your age?")
age = int(input("Your age?"))
if age <= 13:
print("sorry, paws is hiding")
elif careful != "yes" "no":
print("nonsense, say yes or no")
else:
print("Be careful! she nibbles, with no teeth")
else:
print("you have to be careful!")
thanks
Instead of,
elif careful != "yes" "no":
You can use
elif careful not in ("yes" , "no"):

Why is my "or" statement not working as I want? [duplicate]

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
How to check variable against 2 possible values?
(6 answers)
Closed 7 months ago.
import time
repeat=0
price=0
print("Welcome to McDonald's self order system")
time.sleep(0.5)
name_person=str(input("\nPlease enter your name to continue: "))
while True:
order=int(input("\n---Menu---\n 1.Burger\n 2.McPuff\n 3.Ice Cream\n 4.Cold Drink\n\nPlease order by typing the number: "))
if order in range(1,4) :
repeat=str(input("Do you want to order more? Yes/No: "))
if repeat == "No" or "no":
print("Ok")
break
else :
print("\n!!Invalid input!!")
time.sleep(0.5)
The or command is not working when I am typing No it shows Ok that's normal but if I type Yes it should loop but then also it's showing Ok and when I am typing anything it's giving the output Ok but if I am removing the or its working perfectly.
Instead of if repeat == "No" or "no", use:
if repeat == "No" or repeat == "no"
Or even:
if repeat in ("No", "no")

If statement to sense if variable contains certain strings wont work [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 3 years ago.
So I am trying to make a simple calculator. I want it so when I ask for input on what operation they want for the two numbers they type to be add, subtract, multiply, or divide. If the input for the operation selection isn't anything other than add, subtract, multiply, or divide then I want it to print("Incorrect Operation"). I had this working before but I had to rewrite the whole thing cause I accidentally deleted the file.
Anyways, I try to achieve this by using and if statement using the == sign to check if the input string is any of the following strings. That part works. The part that seems to be failing is the else statement which seems to not be picking up that anything other than the specified strings should do:
print("Incorrect Operation")
quit()
The output that I get from the script below does not have any errors but does not do what I just specified (to print something and quit the program). If anyone could help me it would be greatly appreciated!
if function_type == 'add' or 'subtract' or 'multiply' or 'divide':
print("Correct Operation!")
else:
print("Incorrect Operation")
quit()
num_1 = float(input("First Number: "))
num_2 = float(input("Second Number: "))
if function_type == "add":
print(num_1 + num_2)
if function_type == "subtract":
print(num_1 - num_2)
if function_type == "multiply":
print(num_1 * num_2)
if function_type == "divide":
print(num_1 / num_2)
Select the type of operation (add, subtract, multiply, divide): nworfr
Correct Operation!
I'm afraid that is not how boolean expressions work. They seem logical cause it sounds like how you would translate into english but unless you explicitly specify the conditions, the values will be evaluated on their own. You would need to change your condition to:
if function_type == 'add' or function_type == 'subtract' or function_type == 'multiply' or function_type == 'divide':
print("Correct Operation!")
else:
print("Incorrect Operation")
quit()
When you mention the string alone, it will evaluate 'subtract' and not funtion_type == 'subtract'. Since the string is not None, it will always be evaluated to True.

Python check if input number is string [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 4 years ago.
When I run the below code, it always returns Number please no matter what I enter. Why does this happen?
number = input("please type number: ")
if type(number) != int:
print('Number please')
else:
print('number')
Try this, this should work:
string = input()
if string.isalpha():
print("It is a string. You are correct!")
elif string.isdigit():
print("Please enter a string.")
else:
print("Please enter a proper string.")
The .isalpha is used for checking if it's a string. The .isdigit is used for checking if it's a number.
Hope it helps :)

Python fails to match a predefined string with a string entered from keyboard for same values.Is there some buffer logic that i'm missing here? [duplicate]

This question already has answers here:
Why does comparing strings using either '==' or 'is' sometimes produce a different result?
(15 answers)
Closed 5 years ago.
choice = 'yes'
while choice is 'yes':
choice = input("Enter a value for choice : ")
# when i input 'yes' from keyboard for choice, it falls to else block.
if choice is 'yes':
print("As expected")
break
else:
print("Need Help !!!")
I have even tried to match with 'yes\r', as we press 'enter' after our input from keyboard, it still failed to match. Need some insight.
The is keyword is a test for object identity while == is a value comparison. Thus, you needed to change your is to ==.
choice = 'yes'
while choice == 'yes':
choice = input("Enter a value for choice : ")
if choice == 'yes':
print("As expected")
break
else:
print("Need Help !!!")

Categories