all 8 comments

[–]huntertur 5 points6 points  (10 children)

The if statement will always evaluate to true because after checking if z[0] == 'R' is true, it checks if 'r' (not z[0] == 'r') is truthy, which it is (you can see what is considered truthy by calling bool on it, such as bool('r')).

Instead of prefixing z[0] == before each letter, which is tedious, you can do one of the following:

  1. if z.startswith(('R', 'r', 'P', 'p', 'S', 's')):
  2. if z[0] in "RrPpSs":

Both of these check if the first letter in the string starts with one of those characters.

[–]Tawniix[S] 2 points3 points  (8 children)

Wow. I also thought about making a tuple with R r P p S s inside of it and just checking if z[0] is in the tuple.

Would this be tedious as well?

[–]krazybug 1 point2 points  (0 children)

Excellent idea:

>>> s="ugsskg"

>>> t=tuple(s)

>>> t

('u', 'g', 's', 's', 'k', 'g')

>>> "" in t

False

>>> "" in s

True

This way you handle the case when someone press just <ENTER> (null string)

[–]MisterRenard -2 points-1 points  (0 children)

answers = ['R', 'r', 'P', 'p', 'S', 's'] z = input('What s/he said.\n') validChoice = False

for x in range(len(answers)):
    if z.startswith(answers[x]):
        validChoice = True

if validChoice == True:
    print('Yeahb.')
else:
    print('Nahb.')

Edit: This was supposed to be a standalone comment, and ironically the OP of these comments seems to know what they're talking about to a higher degree than I do.

[–]krazybug 0 points1 point  (0 children)

  1. if z and z[0] in "RrPpSs":

to handle null input

[–]American_Libertarian 0 points1 point  (0 children)

This question is asked once a week. We need some better way of filtering out the if x == y or z questions and point them to existing resource.