所以现在,根据我所知,我的代码正在按预期工作。但是,我无法将一个小模块或其他几行放在一起,从用户输入中提取整数并将其膨胀1。你知道吗
dictZero = [ "zero", "none", "null", "nil" ]
dictOne = [ "juan", "one", "won" ]
dictTwo = [ "two", "to", "too", "tu" ]
dictThree = [ "three" ]
dictFour = [ "four", "fore", "for" ]
userInput = input ( "Enter your sentence to inflate: " )
for i in userInput.split():
for e in dictFour:
if e in i:
userInput = userInput.replace ( e, "five" )
for d in dictThree:
if d in i:
userInput = userInput.replace ( d, "four" )
for c in dictTwo:
if c in i:
userInput = userInput.replace ( c, "three" )
for b in dictOne:
if b in i:
userInput = userInput.replace ( b, "two" )
for a in dictZero:
if a in i:
userInput = userInput.replace ( a, "one" )
print ( userInput )
样本输入:
Before we begin to do anything at 1630.
样本输出:
Befive we begin three do anything at 1631.
在不过分复杂化和显著更改代码的情况下,我可以简单地+1输入字符串中的任何数字吗?你知道吗
标准不清楚,我会这样做。你知道吗
运行示例:
如果忽略句末的
1630
,str.replace
只需替换整行的单词,而无需按单词进行拆分。你知道吗如果您还想添加十进制数,则需要逐个字符,这将增加代码的复杂性。你知道吗
输入:
输出:
请注意,这不会在字符串中添加
float
或负值,只添加int
s相关问题 更多 >
编程相关推荐