mardi 8 juin 2021

TypeError: not all arguments converted during string formatting python in if-else loop

This porgram is supposed split a string into two different strings where the even indices are in message1 and odd indices are in message2. There seems to be an error in the code in the if statment in line 7.

input_str = 'bmaunmdbraai'
#Type your code here
message1 = input_str[0]

for i in range(0, len(input_str)):
    if (input_str[i] % 2 == 0):
        message1 = message1 + input_str[i]
    else:
        message2 = message2 + input_str[i]

print(message1, message2)

The error message:

TypeError                                 Traceback (most recent call last)
<ipython-input-31-28323eb4095a> in <module>
      5 
      6 for i in range(0, len(input_str)):
----> 7     if (input_str[i] % 2 == 0):
      8         message1 = message1 + input_str[i]
      9     else:

TypeError: not all arguments converted during string formatting

Please help.

Aucun commentaire:

Enregistrer un commentaire