0

A string has different types of elements such as strings, integers, and floats. I have to change certain elements but not others. String changes to all lower case, and integers change to negative. Floats stay the same.

I've tried a few things and they haven't worked at all. This code isn't even close but I figured I show something.

newint = []
for i in input:
        if type(i) in input == str:
            i.lower()
            return i
        elif type(i) in input == int:
            newint = i * -1
            return i

if input = ["Hello",  33, 3.14, "Joe", -2] at the beginning,
        then at the end input=["hello", -33, 3.14, "joe",  2]
B. Go
  • 1,436
  • 4
  • 15
  • 22
  • 1
    Welcome on stackoverflow please check https://stackoverflow.com/conduct . If you want some help please define what you are trying to achieve with some examples. it will be a lot easier to get an answer then – studioj Sep 22 '19 at 20:55

2 Answers2

0

This should do the trick

input = ["Hello", 33, 3.14, "Joe", -2]

for i in range(0, len(input)):
    if type(input[i]) is int:
        input[i]=-input[i]
    if type(input[i]) is str:
        input[i]=input[i].lower()
print input

you have to use index to access and replace in the input list for i in input: will not get the job done.

Have a look at Immutable vs Mutable types

PilouPili
  • 2,601
  • 2
  • 17
  • 31
0

I have tried to explain in simple words:

input_items =  ["Hello", 33, 3.14, "Joe", -2]
output_items = []

for item in input_items:
    if "int" in str(type(item)): # Check for integers
        if item < 0: # Check if number is negative
            item_int = item * -1 # Multiply by -1
            output_items.append(item_int)
        else: # Item is positive
            output_items.append(item)

    elif "float" in str(type(item)): # Check for floats
        output_items.append(item)

    elif "str" in str(type(item)): # Check for string
        item_lower_case = item.lower()
        output_items.append(item_lower_case)

    else: # Some other data type, just add it to output
        output_items.append(item)



print(output_items)
Stack
  • 4,116
  • 3
  • 18
  • 23