2016-11-13 48 views
-8

如何让用户(输入)从一个句子/字符串中选择一个单词,然后使用python 3.4.3打印出所选单词的位置???如何让用户从字符串中选择一个单词

+0

你尝试过什么吗? – Fejs

+2

你写了一些代码来做到这一点。所以SO不在这里为你做功课。 – jonrsharpe

+0

真的不知道该怎么说才是诚实的,而不是作业 –

回答

1
>>> foo = 'This is a sentence' 
>>> foo 
'This is a sentence' 
>>> choice = input('Choose a word from the sentence ') 
Choose a word from the sentence is 
>>> foo = foo.split() 
>>> foo 
['This', 'is', 'a', 'sentence'] 
>>> if choice in foo: 
...  print(foo.index(choice)) 
... 
1 
>>> 

开关从列表回字符串

>>> foo = ' '.join(foo) 
>>> foo 
'This is a sentence' 
>>> 
+0

非常感谢你,非常感谢 –

+0

下一次,尝试一点研究,检查一些教程.. –

+0

好的队友,试着看,但不知道要搜索什么 –

相关问题