我如何检查输入中是否有字符串 - 初学者

时间:2014-10-16 15:56:39

标签: python

例如:

print ('Choose your character, Bob, a 19 year old student, or Craig, a 32 year old man')
resp = input

if ('Bob') in resp:
    print ('You have chosen Bob.')
elif ('Craig') in resp:
    print ('You have chosen Craig!')
else:
    print ('Invalid. Please check spelling')

请帮助,我正在使用Python进行实验;)

1 个答案:

答案 0 :(得分:0)

首先,您需要实际调用输入函数。其次,您的格式将导致IndentationError

现在回答你的问题。 input将始终返回一个字符串,我想您正在询问如何检查其值。

试试这个:

resp = input('Choose your character, Bob, a 19 year old student, or Craig, a 32 year old man')
characters = ['Bob', 'Craig']

if resp in characters:
    print('You have chosen {}'.format(resp))
else:
    print('Invalid. Please check spelling')