find number in string, add 1 and replace

与世无争的帅哥 提交于 2019-12-11 04:15:02

问题


I need to write a code where a string contains a number, then this number is incremented by 1, and then printed out within the initial string input.

It should work like this

>>>addNumber('I slept 3 hours')
what number would you like to increment? 3
I slept 4 hours
>>>addNumber('I have 366 friends on facebook')
what number would you like to increment? 6
I have 377 friends on facebook

so far I have this, but I know it is wrong and honestly I don't know how to do this.

def incrementNumbers(statement):
    number1=(int(input('What number would you like to increment?')))
    number2 = number1 +1
    i=[]
    for numbers in statement:
        if numbers.isdigit():
            i.append(numbers)
            statement.replace(numbers,number2)

回答1:


Possible error in your code is:

  1. you are trying to replace with int type data in the string directly.
  2. Also only checking numbers.isdigit() will modify all numbers irrespective of what you specified to modify.

Possible corrections to your code to make it run properly:

def incrementNumbers(statement):
    number1 = int(input('What number would you like to increment?'))
    number2 = number1 +1
    i=[]
    for numbers in statement:
        if numbers.isdigit() and int(numbers) == number1:
            i.append(numbers)
            statement.replace(numbers,str(number2))

You can also change your incrementNumbers(statement) function to:

def incrementNumbers(statement):
    number1 = input('What number would you like to increment?')
    number2 = str(int(number1)+1)
    statement = statement.replace(number1,number2)
    return statement

which I think is better than to use a for loop.




回答2:


You can use re.sub here

>>> import re
>>> s = 'I have 366 friends on facebook'
>>> def addone(obj):
...     val = int(obj.group(1))
...     return str(val+1)
... 
>>> i = raw_input('what number would you like to increment?')
what number would you like to increment?6
>>> re.sub('({})'.format(i),addone,s)
'I have 377 friends on facebook'

Another Example

>>> s = 'I slept 3 hours'
>>> i = raw_input('what number would you like to increment?')
what number would you like to increment?3
>>> re.sub('({})'.format(i),addone,s)
'I slept 4 hours'

And you can define your function like

def addNumber(s):
    def addone(obj):
        val = int(obj.group(1))
        return str(val+1)

    i = raw_input('what number would you like to increment?')
    return(re.sub('({})'.format(i),addone,s))

However I advice that you have a validation function

i = raw_input('what number would you like to increment?')
if 0<int(i)<9:
    return(re.sub('({})'.format(i),addone,s))
else:
    return "Number out of range"

This will prevent the user from entering values outside your range




回答3:


Under your requirements that the numeric value must be between 0 and 8, you can use string.replace

def increment(s, inc):
    return s.replace(str(inc), str(inc + 1))

>>> s = 'I have 366 friends on facebook'
>>> inc = 6
>>> increment(s, 6)
'I have 377 friends on facebook'


来源:https://stackoverflow.com/questions/30717634/find-number-in-string-add-1-and-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!