Splitting input into two with for-loop

前端 未结 2 1009
故里飘歌
故里飘歌 2021-01-25 16:45

This is where I got stuck, trying to learn Python using web-course.

Write a program that takes a single input line of the form «number1»+

相关标签:
2条回答
  • 2021-01-25 17:30

    Use str.partition:

    line = raw_input()
    num1, _, num2 = line.partition('+')
    print(int(num1) + int(num2))
    

    If you can't use str.partition and want to use a for loop, then enumerate should help:

    for i, c in enumerate(line):
        if c == '+':
            # what goes here?
    
    0 讨论(0)
  • 2021-01-25 17:30

    Answer

    S = input()
    for position in range(0, len(S)):
    plus=S[position]
        if (plus!="+"):
          continue
        number1=int(S[0:position])
        number2=int(S[position+1:len(S)])
        print(number1+number2)
    
    0 讨论(0)
提交回复
热议问题