• Register
1 vote
22 views

Problem:

I am new in pyhton programing language . I was trying to check if two strings are anagrams python. But I fail repitedly. The code I wrote to check if two strings are anagrams is:
def IsAnagram(str1,str2):
    

str1 = input("Enter your first string : ")
str2 = input("Enter your secoend string : ")

if IsAnagram(str1,str2):
    print("Your first and secoend strings are anagram")
else:
    print("Your first and secoend strings are not anagram")

Which gives following error:

File "ppp.py", line 4
    str1 = input("Enter your first string : ")
    ^
IndentationError: expected an indented block

What is the problem can anyone tell?

10 7 2
6,050 points

1 Answer

1 vote

Answer:

In pyhton if you define a class or function you have to return some of its value. So error is you did not return any value after defining a class or function.You should add:

return sorted(str1) == sorted(str2)

 This line after :

def IsAnagram(str1,str2):
    

This line

So your code should be like:

def IsAnagram(str1,str2):
    return sorted(str1) == sorted(str2)

str1 = input("Enter your first string : ")
str2 = input("Enter your secoend string : ")

if IsAnagram(str1,str2):
    print("Your first and secoend strings are anagram")
else:
    print("Your first and secoend strings are not anagram")

Happy coding.

11 6 4
34,930 points

Related questions

0 votes
1 answer 12 views
12 views
I am new to python programming.. I was trying to check if a string is empty or whitespace in a code : Mystr= "" Yourstr = " " print ("This string is empty : ", end = "") if(not Yourstr): print ("Yes") print ("This string is empty : ", end = "") if(not Mystr): print ("No") It’s output is : This string is empty : This string is empty : No Its not giving me actual result what can I do now?
asked Mar 16 LizzyM 6.1k points
0 votes
1 answer 16 views
16 views
I am new to python programming I was trying to check if a letter is uppercase in python. I wrote some code to check this statement : import string def UpperCase(s_str): for x in s_str: if x not in string.UpeerCase: return False return True But I can not get the actual ... last): File "<stdin>", line 1, in <module> NameError: name 'LoWWE' is not defined Can anyone help me to solve this thing?
asked Mar 14 LizzyM 6.1k points
0 votes
1 answer 32 views
32 views
I am new to python programming. I was trying to compare two strings, I wrote a code to compare: YourStr = "one" MyStr = "one" if YourStr = MyStr print("Two strings are same") else print("Two strings are not same") But it gives an error: File "main.py", line 4 if YourStr = MyStr ^ SyntaxError: invalid syntax Whats wrong in my code and how can I compare two string but it is not case sensitive?
asked Mar 16 LizzyM 6.1k points
1 vote
1 answer 50 views
50 views
Problem: I wrote a program in python to check if two numbers are equal without using arithmetic and comparison operators. The code I wrote is: def Same(a, b): while (a & b != 0): summon = a & b a = a ^ b b = summon << 1 return a if Same( ... . So, I can not check if two numbers are equal without using arithmetic and comparison operators. Can anyone provide me any code of this. Thanks in advance.
asked Apr 22 samhaz 5k points
1 vote
1 answer 315 views
315 views
Problem: I am getting the following error : error: array initializer must be an initializer list or string literal For the array; char x[80] = GetString(); Can any body explain or help?
asked Apr 2 LizzyM 6.1k points