cdegen
cdegen

Reputation: 3

If else command in python: Is it possible to check character by character resulting in one output?

g = raw_input("What gender would you prefer Male or Female? (Please press m or f): ")
p = raw_input("Please enter your 10 digit Phone Number: ")
reasons = raw_input("Please enter your reason by pressing 1-4: ")
r = reasons
valid_mp3 = g+p+r
print valid_mp3
f12312312345

output is in the form of: gender (f) phone# (1231231234) Reason (5) Is there any way to state:

if getFirstCharacter(valid_mp3) == 'f':
    # miscellaneous command
if getSecondCharacter(valid_mp3) == '1'
    # miscellaneous command 

How to implement getFirstCharacter and getSecondCharacter for instance?

Upvotes: 0

Views: 1106

Answers (2)

Chris Doggett
Chris Doggett

Reputation: 20757

Just work against g and p, unless there's some reason you can't.

if g == 'f':
    # do something for females
else if g == 'm':
    # do something for males
else:
    # invalid input

Same thing with the first character of p:

if p[0] == '1':
    # do something for first character of '1'

No sense in trying to do anything complex with the concatenated version if you already have the split versions.

Upvotes: 2

user2286078
user2286078

Reputation:

You could probably use string indices like this:

if valid_mp3[0] == 'f':
    miscellaneous command
if valid_mp3[1] == '1':
    some other command

Upvotes: 0

Related Questions