Problem with "If"/"else" statement in python [duplicate] - python

This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 3 years ago.
I am just getting started learning, and I've been trying for awhile to get this done but I don't know what I am doing wrong
name = input("Enter name :")
if name is "Uber":
print("Hello there, General "+ name ,",you are a bold one!")
else:
print("Begone, " + name , ", you do not belong here.")
I want it to say "Hello there, General Uber, you are a bold one!" if the input is Uber, otherwise it must say "Begone (name),you do not belong here."

is is used for comparing the identity of objects, and the input and "Uber" are not the same object.
To test for equality, use ==:
if name == "Uber":

Related

Python character name [duplicate]

This question already has answers here:
print variable and a string in python
(6 answers)
Closed 10 months ago.
First I assigned a variable
character_name = "Patrice"
On the print function, I wrote
print( " Patrice does a wonderful job at his workplace ")
Instead of writing Patrice on the print function, I wanted to declare the variable that would print Patrice, How would I do it?
There's various ways to achive this, you might want to look at Pythons f-strings:
print(f'{character_name} does a wonderful job at his workplace')

Python: Help me, what am i doing wrong? [duplicate]

This question already has answers here:
String comparison in Python: is vs. == [duplicate]
(4 answers)
Closed 2 years ago.
I'm learning how to code and to start i want crate a command that when i write "Dio" for example it writes me "Error 404: dio non esiste" but it says that the code is wrong, what am i doing wrong? here's the code
name = int(input("Come ti chiami? "))
if name is "Antonio":
print("Eh no")
if name is "Dio":
print("Error 404: Dio non esiste")
if name is "Dio porco":
print("lol")
You have taken input of type int:
Instead just take :
name = input("Come ti chiami? ")
which would be string by default,
and use "==" to compare.
But if you have just started learning, I would suggest first go through the python documentation which would give you a basic understanding of how to write python codes.
is checks if two values are exactly the same reference. Since you're inputting one value from a user and the other is hard coded, they won't be the same reference. Instead, you should use the == operator. Additionally, if you're inputting a name, you shouldn't convert the input to an int:
name = input("Come ti chiami? ")
if name == "Antonio":
print("Eh no")
if name == "Dio":
print("Error 404: Dio non esiste")
if name == "Dio porco":
print("lol")

In '<string>' requires string as left operand, not list [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 3 years ago.
How can I make Python quit if a list contains a particular word?
text = input("Input text: ")
if["exit","quit","close"] in text:
Quit()
It's the other way around
if text in ["exit","quit","close"]:
You can read it as pseudo English, you are checking if the string is in the list.
If you want to check if part of the input is in the list you can use any
if any(x in ["exit", "quit", "close"] for x in text.split(' ')):
You've got it backwards. Try
if text in ["...","..."]:
Think of it as "If this is in that".

Checking to see if a name is in a notepad text document for an A.I system. [duplicate]

This question already has answers here:
How to read a file without newlines?
(12 answers)
Closed 5 years ago.
I have looked over the answers given on other questions. But they are not making any sense to me sorry. I have a notepad document that I want to check to see if the name that you give the program is in the document and if it is. it says hello and let you access the program and lets you do stuff. If your name is not it the program I want it to say that you do not have an account and ask you if you want to make an account and if you say yes then you give it your name and it adds it to the text document. The problem that I am having is that it does not see if your name is in the text document but when you print the names it gives you them and they look like this. 'Coryn\n' and I have no clue what this means. My code is down below.
def AI():
name = raw_input("Hello my name is Cora I am an artificial helper. What is your name? ")
yes1 = ""
name_list = list(open('r','\\\\ph-fss1\\Students\\S39055\\Desktop\\names.txt'))
for names in name_list:
if name in name_list:
print "Welcome back sir"
else:
yes1 = raw_input("You do not have an account do you wish to make one? yes/no ").lower()
if yes1 == "no":
break
if yes1 == "yes":
name('w',open('\\\\ph-fss1\\Students\\S39055\\Desktop\\names.txt'))
You need to split the lines to make them 'readable' for the AI. str.splitlines Docs

Python 3.6.2 Equality Comparison with Boolean Literal [duplicate]

This question already has answers here:
Comparing True False confusion
(3 answers)
Closed 5 years ago.
As part of an assignment we've been asked to create a very basic/elementary program that asks for user input (whether they desire coffee or tea, the size, and whether they desire any flavourings), and then outputs the cost of the particular beverage, including their name and what they ordered, in addition to the cost. The code I wrote works perfectly; however, the only question I have is more for my own understanding. Our instructions for the customer's name were as follows: "The customer’s name – A string consisting of only upper and lower case letters; no
spaces (you may assume that only contains letters of the alphabet)."
Thus my code was as follows:
customerName = str(input('Please enter your name: '))
if customerName.isalpha() == False:
print('%s is an invalid name, please try again!' % customerName)
else:
And then I just continue from there - however, PyCharm is telling me "expression can be simplified - this inspection detects equality comparison with a boolean literal" regarding the
if customerName.isalpha() == False:
statement. What would be the best way to simplify this?
You can use the result of str.isalpha directly; it's a boolean!:
if not customerName.isalpha():
print('%s is an invalid name, please try again!' % customerName)

Categories