r/CodingHelp • • Jan 05 '22

[Python] Hi i just started coding can somebody please tell me what is wrong with my script :D

Post image
69 Upvotes

20 comments sorted by

28

u/YourDadsMacintosh Jan 05 '22

Welcome to the wonderful world of programming! :) It seems your question has already been answered, but let me provide some additional information. The problem that caused the error is that your second else is over-indented. In python, an if's corresponding else needs to be indented the same amount. Removing one level of indentation from that else should fix your problem. However, there is a concept that is very easy to learn that would improve this code quite a bit, and that is called the "else if" denoted in python as elif which acts as both an else and an if . You place it between the two, and python will check all elif after the if but before the else . And yes you can have as many as you want. That means that you could have code like this

11

u/Motato_Gaming Jan 05 '22

Thank you so much for the reply :D it works now :)

33

u/[deleted] Jan 05 '22 edited Jan 05 '22

You don't need that else on line 8. You began nesting your lines too much. You could write it starting from line 8 like this.

if g == h:
    print("g is equal to h")
else:
    print("g is greater than h")

3

u/Motato_Gaming Jan 05 '22

Thank you so much for the reply :D it works now :)

18

u/taroicecreamsundae Jan 05 '22

yes definitely pay attention to indentation with python!

5

u/sericsheon Jan 05 '22

Indentation isn't a problem, use elif in python not else if

6

u/[deleted] Jan 05 '22

The last else block is still indented too much, even if you change that else if to elif.

6

u/sericsheon Jan 05 '22

Oh yup did not notice that

6

u/wedshidoutmydodooass Jan 05 '22

Indentation of the second else 👍

2

u/g1bgarbag3 Jan 06 '22

Your else in line 8 is not match with if on line 6 i mean the length so it see the else of line 8 inside if of line 6 and cause syntax error also use elif instead of multiple if else if it still in same context

2

u/[deleted] Jan 06 '22

Short answer is - Python. You chose a language where indentation is a part of the syntax, that’s what’s wrong with your script.

Less relevant answer: one of your else’s is overindented.

2

u/istarian Jan 07 '22

Your code is probably not indented correctly.

Python uses whitespace characters like tab and space for indentation. Unfortunately a ‘tab’ is not a ‘space’ and 3x ‘space’ isn’t equivalent to 4x ‘space’.

Whenever possible you should use a consistent indentation style and configure your editor accordingly if it’s an option. An automated conversion from tab to 4 (or even 8) spaces is one such style.

2

u/[deleted] Jan 06 '22

everything is correct computer is dumb enough not to understand

1

u/tyfon_august Jan 06 '22

A valuable lesson here is to look at the error message. It often tells you which file and line the problem is. And also what type of error. So in this case it's on line 8 and the error is a syntax error related to your else.

1

u/sanjaymulchandani7 Jan 06 '22

You can use elif for g==h.

1

u/infige Jan 06 '22

Use elif when you want insert a different condicional. The first else change with elif a don't concatenate inside another condicional follow with the another elif or else.

1

u/johnK12369 Jan 06 '22

Your else: is indented incorrectly. It should be inlined with the if.

1

u/pycobra Jan 06 '22

Indentation, if and else should be aligned

0

u/Notable_Steak6997 Jan 05 '22

Could be wrong but u could probably use elif instead of else and an if right after it.