-1
a = input("input a name ")
print("your name is: ",a)
print(a[-1])
if a[-1] == "e" or "a":
    print(a, "there is either a e or a in the name")
    print(a[-1])
elif a[-1] == "s":
    print(a,"there is s ")
else:
    print(a,"there isnt e or a in the name")

Dont understand what I am doing wrong, no matter what u input it will always get stuck on the first if statement

JuLLe Rq
  • 9
  • 5
  • @deceze `a[-1] == "e" or "a"` this will be evaluated as `a[-1] == 'e' and 'e' or 'a'` according to operator chaining right? – Ch3steR Mar 18 '20 at 14:51
  • @Ch3steR It will be evaluated as "does the last character equal 'e' or is 'a' a truthy value". Not much to do with operator chaining. – deceze Mar 18 '20 at 14:53

1 Answers1

1
if a[-1] == "e" or "a"

always evaluates to True because "a" is truthy and you are not testing the equality of a[-1] with "a", simply the value "a" itself. Instead, you mean to write:

if a[-1] == "e" or a[-1] == "a"

An alternative, preferable solution could be:

if a[-1] in ["e", "a"]

which tests whether the last character is in the list ["e", "a"].

dspencer
  • 4,297
  • 4
  • 22
  • 43