Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
propper formating
#1
Hello, I am learning python3 using the python crash course book and I have decided to try to learn the most accepted way to properly format my code early on.
To do this I installed pycode with PIP.

Is it ok to be using 90 characters instead of 80? I find it a lot easier to work in 90 although I understand 80 is the standard, although there seems to be some debate over it when I search it online. I was thinking 90 is a small increase and may be accepted more among other programmers.

I'm doing the guest list tutorial in the crash course book, if your curious this is how my code looks. I hope it is somewhat proper and clean looking for my early stages. It is working at least.
# guest list exercise at the end of the list section
# see intro list for how to'

print('Guest list exercise v1.0')

print("\nI should have room at the venue for three \
people")

guestlist = ['dracula', 'zelda', 'luigi']
# original guest list'
# names = guestlist.title()'

print("\nI think I will invite the following")
print(guestlist[0].title() + ", " +
      guestlist[1].title() + ", and " +
      guestlist[2].title() + ".")

# cant figure out how to print the list without the
# brackets yet and as a title all at once. Will have to
# go back.  For now calling each one separate'

invmsg = (", would you like to cometo dinner \
tonight? \nI am inviting ")

inv1 = ("\nHello " + guestlist[0].title() + invmsg +
        guestlist[1].title() + ", and " +
        guestlist[2].title() + " as well. ")
print(inv1)

# case sensitive
# i will go back and make this code better

inv2 = ("\nHello " + guestlist[1].title() + invmsg +
        guestlist[0].title() + ", and " +
        guestlist[2].title() + " as well. ")
print(inv2)

inv3 = ("\nHello " + guestlist[2].title() + invmsg +
        guestlist[0].title() + ", and " +
        guestlist[1].title() + " as well. ")
print(inv3)

# sent the first three invitations
# one of the guest cant make it

mcmake = ("\nI received a call that ," +
          guestlist[2].title() + ", can't make it to the" +
          "dinner.  \nI will need to invite someone else" +
          "notify the others.")

# \n can be used anywhere in the print text

print(mcmake)

rguest = guestlist.pop(2)
guestlist.insert(2, 'samus')
nguest1 = guestlist[-1].title()

cmmsg = ("I am very sorry but ," + rguest.title() +
         ", can't make it to the party. I invited , " +
         nguest1 + ", instead to join us.")

g1 = guestlist[0].title()
g2 = guestlist[1].title()

print("\nDear " + g1 + ". " + cmmsg)
print("\nDear " + g2 + ". " + cmmsg)

# print(guestlist) this is a tester

# correct  - inv3 has to be updated

inv3 = ("\nHello " + guestlist[2].title() + invmsg +
        guestlist[0].title() + ", and ," +
        guestlist[1].title() + ", as well. ")

print("\n" + inv3)
print("\n I found a even bigger venue, i can invite \
three more people to the dinner")

guestlist.insert(0, 'gandolf')
guestlist.insert(2, 'yoshi')
guestlist.append('cain')


g1 = guestlist[0].title()
g2 = guestlist[1].title()
g3 = guestlist[2].title()
g4 = guestlist[3].title()
g5 = guestlist[4].title()
g6 = guestlist[5].title()

# dont know how to do them all separately
# or to tell it when it ends automatically

inv1 = ("\nHello " + g1 + invmsg + g2 + ", " +
        g3 + ", " + g4 + ", " + g5 + ", and ," + g6 +
        ", as well.")

inv2 = ("\nHello " + g3 + invmsg + g1 + ", " +
        g2 + ", " + g4 + ", " + g5 + ", and ," + g6 +
        ", as well.")

inv3 = ("\nHello " + g5 + invmsg + g1 + ", " +
        g2 + ", " + g3 + ", " + g4 + ", and ," + g6 +
        ", as well.")

print(inv1)
print(inv2)
print(inv3)

print("\nSo for dinner I have invited these fine " +
      "people. " + g1 + " , " + g2 + " , " + g3 + " , " + g4 +
      " , " + g5 + " , and " + g6)

print("\nDue to a unforeseen incident at the \
location of the dinner hall, I now have to have\
the dinner at home and can only invite two \
guest.")

print("\nI think I will only invite " + g2 + " and " +
      g6 + " to dinner, I will need uninvite the" +
      " other right away")

removedguest = guestlist.pop(0) + " ,"\
                        + guestlist.pop(1) + " ,"\
                        + guestlist.pop(1) + "  "\
                        + guestlist.pop(1)

# im sure there is a better way to do this
print("\nI have uninvited the following people \
form the party: " + removedguest.title())

uninvmsg = "I am sorry to have to inform you due\
to unforeseen events I must uninvite your from \
the dinner party. \nI will make sure to invite \
you next time."

print("\nDear " + g1 + ", " + uninvmsg)
print("\nDear " + g3 + ", " + uninvmsg)
print("\nDear " + g4 + "," + uninvmsg)
print("\nDear " + g5 + ", " + uninvmsg)

stillinvmsg = "I just wanted to let you know you\
are still invited to my dinner party, hope to see\
you there."

print("\nDear " + g2 + ", " + stillinvmsg)
print("\nDear " + g6 + ", " + stillinvmsg)

del guestlist[0]
# removing the last two names
del guestlist[0]
# print(guestlist) test line
# exercise finished
Reply
#2
There are guidelines, and there are rules. Rules should not be broken and there are generally good reasons for rules. Guidelines are "best practice". 80 chars is a guideline, has historic basis (ok, yes, I did program using punch cards that had 80 columns) and enhances readability. Would not sweat it.
Reply
#3
(Jul-15-2021, 07:59 PM)paracelsusx Wrote: I have decided to try to learn the most accepted way to properly format my code early on.

Good start would be to read and follow PEP 8 -- Style Guide for Python Code. It states quite categorically:

Quote:Limit all lines to a maximum of 79 characters.

For flowing long blocks of text with fewer structural restrictions (docstrings or comments), the line length should be limited to 72 characters.

However, one should consider it in context with A Foolish Consistency is the Hobgoblin of Little Minds

Regarding formatting your code - I saved it into file and my text editor complained about every blank line ('blank line contain whitespace').

There are formatted string literals (f-strings) for app 5 years now and they are much more 'human readable'. Also, if you enter manually list of names, why not enter them correctly instead of using .title over and over again? Also you should use datastructures like lists or sets which will significantly reduce lines of code and will make code much more readable.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formating generated .data file to XML malcoverc 3 1,347 Apr-14-2022, 09:41 PM
Last Post: malcoverc
  Adding graph points and formating project_science 4 2,379 Jan-24-2021, 05:02 PM
Last Post: project_science
  Excel: Apply formating of a sheet(file1) to another sheet(file2) lowermoon 1 2,024 May-26-2020, 07:57 AM
Last Post: buran
  tuple and formating problem darktitan 7 3,371 Feb-17-2019, 07:37 PM
Last Post: marienbad
  pymysql: formating ouput of query wardancer84 18 8,308 Oct-04-2018, 01:54 PM
Last Post: wardancer84
  output formating mcmxl22 2 3,059 Jun-19-2017, 07:21 PM
Last Post: buran

Forum Jump:

User Panel Messages

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