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")
Related
This question already has answers here:
Identifying the data type of an input
(4 answers)
Closed 2 years ago.
I'm unable to compare users input as str or int in following code..
A=input(" ")
if type(A)=str:
Print("this is string")
elif type(A)=int:
Print("this is integer")
So please tell me how to deal with this type of problem
use string class isdigit() method to check user input is number or string.
This question already has answers here:
How do you input integers using input in Python [duplicate]
(3 answers)
Closed 3 years ago.
I keep getting this error:
if AR_GCC<16 and AR_GCC>0:
TypeError: '<' not supported between instances of 'str' and 'int'
It is a program that is supposed to print the cancer risk according to the input of the user (not a real thing just for practice)
This is the piece of code that doesn't work:
AR_GCC=input("AR_GCC repeat copy number?")
if (AR_GCC>0 and AR_GCC<16):
risk="High risk"
elif (AR_GCC >= 16):
risk = "Medium risk"
else:
print("Invalid")
You need to make sure the input is interpreted as number. Simply adding int() before the input function should solve that. Of course if the value passed is a string, it will fail. Therefore your code should be:
AR_GCC=int(input("AR_GCC repeat copy number")) should do the trick. Sorry for formatting, typing from phone
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)
This question already has answers here:
Getting user input [duplicate]
(5 answers)
Closed 5 years ago.
For a challenge I am tasked with creating a unit converter that can change the units. I chose degrees Celsius to Fahrenheit. I am quite new to Python. My problem is that I ask a question on the code e.g.
print("Enter Value: ")
How do I make it so that the value that a user enters becomes the variable f for Fahrenheit which can then be changed to Celsius so I can do this..
print((f - 32) / 1.8)
Can anyone help and explain it in a way a beginner can understand?
Assuming you're using Python3, what you need is:
temp=input("Temperature please?")
print((int(temp)-32)/1.8)
Also, please look up the docs Jacek linked to so that you understand what's really going on here.
Use input() function:
Input and Output Docs
temp = 0
# while loop
# wait until user set a input
while not temp:
# default type in input is "str"
user_input = input("Enter Value: ")
if user_input.isdigit():
temp = user_input
# we know every char is digit
print (((int(temp)-32)/1.8))
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Multiple if statements under one code, with multiple conditons [duplicate]
(3 answers)
Closed 8 years ago.
It is pretty self-explanitory from the code but I want to check if the input is not equal to these values than ask again. I thought this would work but it doesn't and its glitchy, what is a better way to do this?
type=input("Please choose an option: ")
while type.isalpha() == False:
type=input("Please choose an option: ")
while type != ("a" and "A" and "b" and "B" and "c" or "C"):
type=input("Please choose an option: ")
Simply do while not type in ("a","A","b","B" ...) to check whether type is one of the listed elements.
The code above is, as mentioned in comments, equivalent to while type != someListElement because the and and or are evaluated first.
You would need to write:
while (type != "a" and type !="A" and type !="b" and type !="B" and type !="c" or type !="C"):
I think the simplest solution would be to use
type = raw_input("Please choose an option: ")
while type not in list('AaBbCc'):
type = raw_input("Please choose an option: ")
list will convert from a string to a list of single-character strings, which you can then test for inclusion using in. I don't think you need the test for isalpha, because everything you're checking for is already a letter.
Also, you should always use raw_input rather than input to get user input, because raw_input always returns a string, while input tries to eval what the user enters, which is not what you want.
(This is assuming you're using Python 2. If you're using Python 3, input is what raw_input was before, and raw_input no longer exists.)