Python Forum
Password Saver Project
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Password Saver Project
#7
Indenting is fundamental and you really have to get your head around it. I know it seems confusing at first, but after a while it will be obvious.

Many other languages use strict formatting and/or the use of lots of brackets {} to help the computer understand how different statements in code relate to each other.

At its simplest, any statement used to start a "block" of code has a colon at the end and then everything that should happen in relation to that start point is indented by a certain number of spaces or tabs.

Some people (places) prefer to use spaces, some tabs. You need to be consistent. Do not mix them (you can set your editor, in most cases, to convert tabs to spaces). Most commonly, people use either 2 spaces (2 space tabs) or 4 spaces (4 space tabs). Be consistent, always use same spacing size.

If within an indented block of code, there is a statement (sub-statement if your prefer) that also requires some code to be run subsidiary to that statement, then that to will end with a colon and the lines underneath will be further indented.

Here's a simple example, where a every number in a specified range is tested to see if it is a prime number (note that this is not the most efficient approach to that problem).

num = range(2, 20)  # change range to check as desired
for n in num:  # check each n in range to see if is prime
    for x in range(2, n):  # divide each n(umber) by all lower numbers from 2 up
        if n % x == 0:  # if modulo n/x is 0, no remainder, then not prime
            print("{:4d} is NOT a prime number".format(n))
            break  # no point looking at higher values of x
    else:  # nobreak - i.e. what to do if iter completed without match
        print("{:4d} is     a prime number".format(n))  # didn't break, must be prime
print("All done")
Note in particular the else: line, which refers to the end of the inner for loop condition rather than being part of the if condition that it would belong with if it was indented another four spaces.

Here's a useful video illustrating the above.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Messages In This Thread
Password Saver Project - by jhenry - Oct-10-2017, 09:27 PM
RE: Password Saver Project - by sparkz_alot - Oct-10-2017, 09:51 PM
RE: Password Saver Project - by jhenry - Oct-10-2017, 10:08 PM
RE: Password Saver Project - by sparkz_alot - Oct-10-2017, 10:26 PM
RE: Password Saver Project - by gruntfutuk - Oct-11-2017, 09:46 AM
RE: Password Saver Project - by jhenry - Oct-11-2017, 09:55 AM
RE: Password Saver Project - by gruntfutuk - Oct-11-2017, 10:28 AM
RE: Password Saver Project - by buran - Oct-11-2017, 10:33 AM
RE: Password Saver Project - by gruntfutuk - Oct-11-2017, 10:42 AM
RE: Password Saver Project - by sparkz_alot - Oct-11-2017, 12:31 PM
RE: Password Saver Project - by jhenry - Oct-12-2017, 06:38 AM
RE: Password Saver Project - by gruntfutuk - Oct-12-2017, 07:16 AM
RE: Password Saver Project - by jhenry - Oct-13-2017, 07:22 AM
RE: Password Saver Project - by gruntfutuk - Oct-13-2017, 07:35 AM
RE: Password Saver Project - by jhenry - Oct-13-2017, 08:15 AM
RE: Password Saver Project - by gruntfutuk - Oct-13-2017, 08:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python project help (Password manger using mysql) lifeofpy 2 4,882 Jul-31-2020, 06:18 PM
Last Post: deanhystad
  Python Password Saver Assignment sshellzr21 2 6,348 May-02-2020, 01:34 AM
Last Post: sshellzr21
  Password Saver Program suitec 3 9,101 Aug-17-2017, 12:26 AM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020