What's the issue with my code?

There was a question i came arcoss and I am new to python. Was trying it out but still get an error:
You are given a list of strings, using list comprehensions create a new list of strings called ‘my_list’ , which only contain the strings that have odd length.

my solution:
def main():
str_list = [‘given’, ‘intern’, ‘InterviewBit’, ‘network’, ‘local’, ‘multiple’, ‘define’, ‘nodes’, ‘algorithm’, ‘allows’, ‘community’, ‘phase’, ‘single’]
my_list = [x for x in str_list if len(str_list)%2 == 1]

print(my_list)
return 0

if name == ‘main’:
main()

the solution they want is in my_list… pls help me out

It should be len(x) not len(str_list).

1 Like

Use x instead of str_list in len(str_list)

thx

thx :slight_smile:
There is another simple question I am stuck at pls let me know the error,
You are given a string S. Your task is to find out if the string contains: alphanumeric characters, alphabetical characters, digits, lowercase and uppercase characters.

Output Format

In the first line, print True if S has any alphanumeric characters. Otherwise, print False.
In the second line, print True if S has any alphabetical characters. Otherwise, print False.
In the third line, print True if S has any digits. Otherwise, print False.
In the fourth line, print True if S has any lowercase characters. Otherwise, print False.
In the fifth line, print True if S has any uppercase characters. Otherwise, print False.

My solution:
def main():
S = raw_input()
print(S.isalnum())
print(S.isalpha())
print(S.isdigit())
print(S.islower())
print(S.isupper())
#You code goes here

return 0

if name == ‘main’:
main()

All these above functions will check for the whole string and if the whole string satisfies the condition then only they give true. So you need to check for each character.

s=input()
ans = ['False']*5

for i in s:
    if i.isalnum():
        ans[0]='True'
        
    if i.isalpha():
        ans[1]='True'
        
    if i.isdigit():
        ans[2]='True'
        
    if i.islower():
        ans[3]='True'
        
    if i.isupper():
        ans[4]='True'
        
for i in ans:
    print(i)

You should to use
print (any(X.isalnum for x in S))

@src01 Use ``` to format your code like this

def main():
str_list = [‘given’, ‘intern’, ‘InterviewBit’, ‘network’, ‘local’, ‘multiple’, ‘define’, ‘nodes’, ‘algorithm’, ‘allows’, ‘community’, ‘phase’, ‘single’]
my_list = [x for x in str_list if len(str_list)%2 == 1]

print(my_list)
return 0
if name == ‘main’:
main()
1 Like