Python - And, OR validations [duplicate] - python

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.)

Related

how to decide type of a variable in python? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
enter image description here
I wrote a simple code in python.
Originally my assignment is to receive two inputs(name of person) and print them.
Here's my question.
When I try to sum two variables but one of them is int and another one is str, an error occurs.
But in this case (the picture) why variable 'a' is recognized as a str not int?
I think there must occurs an error but a is recognized as a str and work well.
In Python 3, input() always returns a string. (In Python 2, input() would try to interpret – well, evaluate, actually – things, which was not a good idea in hindsight. That's why it was changed.)
If you want to (try to) make it an int, you'll need int(input(...)).

How to give input in any Data Types in Python? [duplicate]

This question already has answers here:
Can the input() function in Python dynamically detect the input's data type?
(3 answers)
Closed 2 years ago.
I have one doubt in the Python Input method.
When I am entering input, it is always considering as String in Python. How to get the Input value in many data types.
As Example:
If I enter Integer value as input then the Code supposed to take that as Integer.
Code:
a=str(input("Enter A Value \n"))
In the above code, it converts my input always as String. Because I used str there.
If I remove str from there and if I type some numbers in the input will it take as an integer?
Python 3.x will always consider input as string you have to type cast each field manually.
int(input('Enter Number'))
str(input('Enter String'))

Python: Case Sensitivity in user input [duplicate]

This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed 2 years ago.
I want to make my user's input (in Python) case insensitive. Like if user type admin, Admin ADMIN or even he or she types like aDMIN. This should be insensitive so i can get exact input
Use lower() : s = input().lower()
Convert your user's input to lower case. Then whatever you compare it to can be lower case.
string = input("Enter something: ")
string = string.lower()
Just use the lower() function, it converts a string to lower case.
myStr = JAMES
myStr.lower() > james
You can just compare it to lower case admin every time.
input_str = input("Enter role: ")
if input_str.lower() == "admin":
pass

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)

How to code Python to accept only integers? [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 8 years ago.
I'm working on a Python 3.4 coding assignment that needs an integer input from the user. I am having trouble figuring out how to make the program accept only integer values; for instance, if the user inputs a float (i.e. "9.5"), the program will output, "That's not an integer! Try again."
Simple, use raw_input to get the string input, then call .isdigit() on it to see if it is an int. If it is, cast it to an int, then check it is within the valid range. Stick it all in a while loop so it keeps being called until a valid number is input, and you're all set.

Categories