我在我的数据结构类中有一个作业,我正在使用 Python 来尝试解决它。我在 Python 中真的被卡住了,生疏了,所以请多多包涵。
问题
Read a sentence from the console.
Break the sentence into words using the space character as a delimiter.
Iterate over each word, if the word is a numeric
value then print its value doubled, otherwise print out the word,
with each output on its own line.
Sample Run:
Sentence: Hello world, there are 3.5 items.
Output:
Hello
world,
there
are
7
items.
到目前为止我的代码...
import string
import re
def main():
string=input("Input a sentence: ")
wordList = re.sub("[^\w]", " ", string).split()
print("\n".join(wordList))
main()
这给了我这个输出:
>>>
Input a sentence: I like to eat 7 potatoes at a time
I
like
to
eat
7
potatoes
at
a
time
>>>
所以我的问题是弄清楚如何提取数值然后加倍。我不知道从哪里开始。
任何反馈都会受到赞赏。谢谢!