+ 1

What is wrong in the else statement ? Code in description

register = {"Aditya":{"Maths1":30,"Maths2":30,"Science1":30,"Science2":30,"History":30,"Geography":30,"Hindi":30,"English":30},"Atharva":{"Maths1":30,"Maths2":30,"Science1":30,"Science2":30,"History":30,"Geography":30,"Hindi":30,"English":30},"Amir":{"Maths1":30,"Maths2":30,"Science1":30,"Science2":30,"History":30,"Geography":30,"Hindi":30,"English":30},"Tanishq":{"Maths1":30,"Maths2":30,"Science1":30,"Science2":30,"History":30,"Geography":30,"Hindi":30,"English":30},"Ratan":{"Maths1":30,"Maths2":30,"Science1":30,"Science2":30,"History":30,"Geography":30,"Hindi":30,"English":30},"Samyak":{"Maths1":30,"Maths2":30,"Science1":30,"Science2":30,"History":30,"Geography":30,"Hindi":30,"English":30},"Varun":{"Maths1":30,"Maths2":30,"Science1":30,"Science2":30,"History":30,"Geography":30,"Hindi":30,"English":30}} a = str(input("Name\n")) b = str(input("Subject\n")) if b == "ALL" or "all" : print(register[a]) else : print(register[a][b]) output :- File "source_file.py", line 9 else : ^ SyntaxE

16th Feb 2021, 3:54 AM
Shlok Jathar
Shlok Jathar - avatar
2 ответов
+ 4
Make sure your TAB space is correct. "else" should be right under "if" word
16th Feb 2021, 4:02 AM
Essisten
Essisten - avatar
+ 4
1. if b == 'ALL' or 'all': # always True A non empty string is truthy. So this is like doing 1 comparison and then the other side is always True. Would be the same as; if b == 'ALL' or True: if b == 'ALL' or b == 'all': # comparison on each side of or Or better yet just use the lower() method if b.lower() == 'all': (the upper() method could be used similarly) 2. Your indentation is off and doesn't match. if b.lower() == 'all': print(register[a]) else: print(register[a][b]) See how the e in else is directly below the i in if, and the p from each print() are also lined up. Their indentation matches. EDIT: also using the str() function for the result of input() is redundant and not needed as input() already returns a str object. Don't use; str(input()) just use; input() Conversion from input() is only needed for types other than string, such as int(), float(), etc.
16th Feb 2021, 6:03 AM
ChaoticDawg
ChaoticDawg - avatar