简体   繁体   English

如果是python中的else命令:是否可以逐个字符地检查导致一个输出?

[英]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: 输出的形式为:性别(f)电话号码(1231231234)原因(5)有没有办法说明:

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

How to implement getFirstCharacter and getSecondCharacter for instance? 例如,如何实现getFirstCharactergetSecondCharacter

Just work against g and p, unless there's some reason you can't. 只是对抗g和p,除非有某种原因你不能。

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: 与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. 如果您已经拥有拆分版本,尝试使用连接版本执行任何复杂操作都没有意义。

You could probably use string indices like this: 您可以使用这样的字符串索引:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM