Why is Python strict about indentation? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This question is coming from this Stack Overflow question. Why is Python is designed to use correct indentation either space or tab? In this way, is it purposefully to serve any benefit?

Indentation is essential in Python; it replaces curly braces, semi-colons, etc. in C-like syntax.
Consider this code snippet, for example:
def f(x):
if x == 0:
print("x is zero")
print("where am i?")
If it weren't for indentation, we would have no way of indicating what code block the second print statement belongs to. Is it under the if statement, the function f or just by itself in the document?

There is no set width of a tab character, so Python has no way of knowing how many spaces to count a tab as.

Related

warning: "statement seems to have no effect" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
PyCharm is highlighting my code in yellow and giving a warning. On hover, it says:
statement seems to have no effect
Can somebody please explain what this means?
You need a single equals sign = to assign a value to punctuacion:
punctuacion = punctuacion + 10
In this case, it'd be even better to use a compound assignment operator, like `punctuation
punctuacion += 10
As is, the == is the equality operator, which just evaluates to an unused value (either True or False). That's useless, hence the warning, the IDE suspects you made a mistake like this.

How to end if statement in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm learning python.
I have touched on other programming languages ​​for example:
PHP, HTML, C #, C ++ etc. But I could not understand Python a bit from the programming languages ​​I listed
Uses {}
When I try to start an if statement in Python I will have to start with : but I can not find a way to end it.
I mean, if the if statement is activated, some code must be executed, so I can not find a way to see which code will be executed when the if statement is activated.
I'm 11 years old.
This might be really bad question for python programers that because I'm currently learning python
In python what you have to understand about block of code, is just indentation. check the following example:
if 10 > 20:
print('Inside if statement')
print('Outside if statement')
And the same goes to loop, function, class,...
In c#, c++, c we use {} to define a block of code but in python is just indentation

Python String Expression format [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am not understanding the meaning of each of the terms in the curly brackets (I was a bit overwhelmed with this source https://docs.python.org/3.4/library/string.html)
def format_money(amount):
return '${:.2f}'.format(amount)
I know that it return this format: $39.99 but I don't know what each sign means. Could someone explain?
the $ is outside of formatted string placeholder
{: is the beginning of the formatted string
.2f is the fixed number of places after the decimal
} end of your place holder
i find it always fun to experiment to test a function or a module, you can also format the date, output, lots of other stuff... all in that lovely reference page

Exit condition block for non-condition, or long block condition? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I always wanted to ask this question. I would like to know what form I should use, and why. What is better:
if my_condition:
# very long block of code with multiple indention levels
OR:
if not my_condition:
exit
# Still here ?
# very long block of code with multiple indention levels
EDIT:
I'm not asking for personal opinion. Is there any PEP recommendation/ any criterion I'm not aware of ?
From the Zen of Python:
Flat is better than nested.
The second approach, if not my_condition: exit, avoids a level of nesting and is therefore superior according to this particular criterion.

I'm not sure of a way to go about this task using Python code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Using Python code I cannot find a simple way to answer this task:
Write a program that randomly generates a 4 digit passcode, then gets
the user to try and guess the passcode. The code must then tell the
user how many digits were correct in there guessed passcode.they can
guess 12 times before they lose or, guess correctly within 12 tries to
win.
Please help write a python code for this task. The main part that I'm stuck on is how to separate the digits to check them without putting spaces between them and then using the split command.
Most likely you are handling things as integers. You can use raw_input to read it in as is:
response = raw_input('Enter your number --> ')
And treat this as a string.
print ("Your first digit is", response[0])

Categories