0

Hello I have been study python for the pass few months and can't seem to figure out how to get the code below to work. The could runs but I am not receiving the output I was expected. I know strings are immutable, at first I kept using list function a realized that was incorrect but im stuck at thE moment. Not sure if I need to use a for loop to iterate through the string?

def function(input_string):
    output_string = ''
    if len(input_string)> 6:
       return input_string[1:] #Should return the string starting at index 1
    elif input_string.count('e') > 2:
       return input_string.count('e') #should return the count of 'E'
    elif input_string.isdigit():
       return input_string % 2  
#Should if string is a digit halve it example input_string = '4224' should be output_string'2114'
    else:
       return input_string[-1:0] #Should reverse the string outoutput_string='Ecetem'
            
    return output_string    

input_string = 'metecE'
output_string = function(input_string)
print(output_string)

I've tried using a for loop. This is a practice question for a test. The criteria after the # are required and it has to be in a function. I will not be allowed to change any of the code beside the logic I have with if-else statement. Any help with be greatly appreciate. I really want to figure out what I did incorrectly.

3
  • 1
    Reversing a string is done by input_string[::-1]. If the start is less than the end, and you did not specify a negative increment, it will return an empty string. When you do input_string[-1:0], Python still wants to do forward indexing. It doesn't try to "sort" the indexes. Commented Nov 2, 2022 at 21:02
  • Thanks Tim, I think i need to spend a little more time review string function. Commented Nov 2, 2022 at 21:26
  • Please describe which of the if-else options are not working and provide a relevant example. Commented Nov 2, 2022 at 21:59

1 Answer 1

1

you had 3 mistakes:

  1. you didn't convert the input_string to int when trying to divide it

  2. you didn't use the right sign for division (/ and not %)

  3. you tried to reverse the input_string using [-1:0] when you need to do [::-1]

def function(input_string):
    if len(input_string) > 6:
        return input_string[1:]
    elif input_string.count('e') > 2:
        return input_string.count('e')
    elif input_string.isdigit():
        # you need to turn input_string to int and then use `/` to divide and not `%` 
        # (if the result to be int use `//`)
        return int(input_string) / 2
    else:
        # use [::-1] to reverse a string and not [-1:0]
        return input_string[::-1]


input_string = '4224'
output_string = function(input_string)
print(output_string)
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you very much, this helps a lot. I am trying to get more familiar with manipulating string. I did have one question, it was suppose to be stored in a empty string so output_string was set to output_string = ' '. The function and the empty string was provide, and I was just suppose to provide the logic. Will this work having the empty string before my logic?
you can add output_string = "", but you are not using the variable output_string in the function so no need for this declaration.
unless you use global statement the variables inside a function and outside a function aren't shared, you can have 2 variables with the same name and each of them will contain something different
And to extend that comment, don't use global statements.
This worked perfectly. Thank you guys so much for the quick response. My headache is gone now lol.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.