开发者社区> 问答> 正文

在python中求和两个或多个数字

我有一个简单的代码

orgtxt = input("Enter text: ")
charA = "a"
charB= "b"
charC="c"

for i in charA:
    orgtxt = orgtxt.replace(i, '2')
for i in charB:
    orgtxt = orgtxt.replace(i, '22')
for i in charC:
    orgtxt = orgtxt.replace(i, '222')

print(orgtxt)

如果输入为(abc),则上述代码结果为(222222)

现在我想对每行加一个特定的数字(30),所以它将执行此操作

orgtxt = input("Enter text: ")
charA = "a"
charB= "b"
charC="c"

for i in charA:
    orgtxt = orgtxt.replace(i, '2')
sum = orgtxt + 30

for i in charB:
    orgtxt = orgtxt.replace(i, '22')
sum = orgtxt + 30

for i in charC:
    orgtxt = orgtxt.replace(i, '222')
sum = orgtxt + 30

print(orgtxt)

我需要它

  • look in the input text for any char (a)

  • replace it with (2)

  • then, add (30)

并继续寻找任何字符(b)

  • replace it with (22)

  • add (30) and so on...

最后,如果输入为(abc),它将显示最终结果为(3252252)

\ * \ * \ * \ * \ * \ * \ * \ * \ * \ * \ * \ * \ * \ *

好的,这是一个清晰的说明

提示用户输入

orgtxt = input("Enter text: ")

定义要在下一步中替换的输入用户文本的字符串

charA = "a"
charB= "b"
charC="c"
charD="d"

依此类推,对于A〜Z中的所有字符,用户输入的任何文本都将替换为特定的数字,该数字将是从201〜227开始的序列号

在这里,我搜索任何字母A的代码,并将其替换为数字(201)

for i in charA:
    orgtxt = orgtxt.replace(i, '201')

那么我需要将结果(201)与特定数字(30)相加

sum = int(orgtxt) + 30

所以这里的结果将是(231)

继续运行代码并搜索任何字母B并将其替换为(202)

for i in charB:
    orgtxt = orgtxt.replace(i, '202')

那么我需要将结果(202)与特定数字(30)相加

sum = int(orgtxt) + 30

所以这里的结果将是(232)

继续运行代码并搜索任何字母C并将其替换为(203)

for i in charC:
    orgtxt = orgtxt.replace(i, '203')

那么我需要将结果(203)与特定数字(30)相加

sum = int(orgtxt) + 30

所以这里的结果将是(233)

代码将为201至227的所有字符A〜Z定义

将每个替换的字符(具有给定的数字)加起来,然后

print(orgtxt)

打印最终结果,例如,如果用户输入为(Ali),则可能为(231242239)

231(A)最初是用数字(201)代替,然后是(30)

242最初被(L)替换为数字(212),然后被总和(30)

239 was originally (i) replaced with number (209) and then sum with (30)

问题来源: stackoverflow

展开
收起
is大龙 2020-03-24 20:32:51 595 0
1 条回答
写回答
取消 提交回答
  • I think you should explain once more.

    you have the example "abc"

    after replacing "a" with "2" you would have

    "2bc"

    What do you mean with adding 30 to "2bc" ?????

    Please explain also what result you'd like to have for the string

    "aabc" and what for "ababbc" ?

    I suggest you explain step by step in your question what you'd like to have. Then it will be easy to write a program for it, but without some examples everybody will probably interpret your question differently and all you get will be wild guesses.

    In fact writing an unambiguous question is rather difficult, but necessary if you want good answers.

    One issue is, that you have to convert a string into a number (an int) before being able to do any arithmetic operation.

    if you have the string "2" (e.g. a = "2" ) and you want to perform operations (add for example a 30 ), then you have to convert it (e.g. rslt = int(a) + 30 )

    Also if you have numbers and you want to concatenate them, then you have to convert them to strings first. so with a=12 and b=54 you had to do str(a) + str(b) to get "1254" .

    Her another attempt for an answer, but I have the impression I completely misinterpreted your question

    请提供示例输入和示例预期结果,并逐步说明。例如,您为所有a仅添加30个还是要为每个a添加30个敌人?您对不是abc的字母做什么? ,...

    orgtxt = input("Enter text: ")
    charA = "a"
    charB= "b"
    charC="c"
    num_to_add = 30
    
    result = ""
    for i in orgtxt:
        if i == charA:
            result = result + str(num_to_add + 2)
        elif i == charB:
            result = result + str(num_to_add + 22)
        elif i == charC:
            result = result + str(num_to_add + 222)
    
    
    print(result)
    

    如果这是您想要的,则以下应该产生相同的结果,不同之处在于,任何非a,b,c的字母将与第二个代码一起保留,而与第一个代码一起删除:

    orgtxt = orgtxt.replace(charA, str(num_to_add + 2))
    orgtxt = orgtxt.replace(charB, str(num_to_add + 22))
    orgtxt = orgtxt.replace(charC, str(num_to_add + 222))
    
    print(orgtxt)
    

    回答来源:stackoverflow

    2020-03-24 20:32:59
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载