Including two things that it will check in a single if statement in python

lets say that I have a program that works like in python

x=raw_input("Please enter name: ")
if x=="David" and "Jonathon"
   print "Whatever"

How can I get two things, such as Jonathon and David, so he excludes both of these scenarios and both prints “Whatever happens.” I know that [“David” and “Jonathon”] are not the proper syntax for this, so I want to know what is. I also know how to do this by adding an elif statement, but I want to find out a different way.

+3
source share
3 answers

You can do:

x=raw_input("Please enter name: ")

if x=="David" or x=="Jonathon":

    print "Whatever"

Or you can do:

names=["Bob","Joe"]
x=raw_input("What is your name?\n")
if x in names:
     print "Whatever"

If you are dealing with big data.

+3
source
names = ["David", "Jonathon"]
x=raw_input("Please enter name: ")
if x in names:
    print "Whatever"
+2
source

. , , , , x == "" x == "", -, .

It is clear that x cannot have two values, therefore AND will never be evaluated as true. This would be the correct code (or version of cyciraptors using an array)

x=raw_input("Please enter name: ")

if x=="David" or x=="Jonathon":

    print "Whatever"

Given the discussion, here's the whole hog using a simple configuration file (I tested conf.read (). Split (), so please, no comments on this). The configuration file will contain one name for each line without spaces (so there is no "last name last name"):

conf = open('/path/to/config.txt','r')
names = conf.read().split()

x=raw_input("Please enter name: ")
if x in names:
    print "Whatever"

If your list of names could become very long, this would be the most convenient solution.

0
source

Source: https://habr.com/ru/post/1788639/


All Articles