0
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

输出形式为:gender (f) phone# (1231231234) Reason (5) 有没有办法说明:

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

如何实施getFirstCharactergetSecondCharacter例如?

4

2 回答 2

2

只对 g 和 p 工作,除非有某些原因你不能。

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

与 p 的第一个字符相同:

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

如果您已经拥有拆分版本,那么尝试对连接版本进行任何复杂的操作是没有意义的。

于 2013-04-23T03:29:26.307 回答
0

您可能可以像这样使用字符串索引:

if valid_mp3[0] == 'f':
    miscellaneous command
if valid_mp3[1] == '1':
    some other command
于 2013-04-23T02:50:25.217 回答