Reputation: 1
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.
Upvotes: 0
Views: 58
Reputation: 567
you had 3 mistakes:
you didn't convert the input_string
to int
when trying to divide it
you didn't use the right sign for division
(/
and not %
)
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)
Upvotes: 1