Exercise 18 "Learn Python 3 the Hard Way"

I am doing exercise 18 (ex18.py) on page 60 of “Learn Python 3 the Hard Way” in my Windows 7 laptop with my Notepad ++ text editor. Here is my script:

# this one is like your scripts with argv
def print_two(*args):
      arg1, arg2 = args
      print(f"arg1: {arg1}, arg2: {arg2}")
# ok, that *args is actually pointless, we can just do this
def print_two_again(arg1, arg2):
      print(f"arg1: {arg1}, arg2: {arg2}")	
# this just takes one argument
def print_one(arg1):
      print(f"arg1: {arg1}")

# this one takes no arguments
def print_none():
      print("I got nothin'.")

print_two("Zed", "Shaw")
print_two_again("Zed", "Shaw")

I get this error displayed on my PowerShell screen:

File "ex18.py", line 4
  print(f"arg1: {arg1}, arg2: {arg2}")

Tab Error: inconsistent use of tabs and spaces in indentation

In my PowerShell, I use:
lpthw> python ex18.py

and I get the above error message

I honestly cannot see where my script deviates from ex18.py script on page 60. Any advice would be greatly appreciated.

The “^” is below the last parenthesis ) on line 4 of my ex18.py script in my PowerShell error message display. For some reason, I cannot get the ^ to be placed there in my forum message to you folks.

Hey, did you indent your code?
We can’t tell by simply watching it here.
Can you please delete all the code, click the sixth icon above the editor when you re-edit your message and recopy your code inside the 2 ` (tick signs)? This way the code will keep it’s original formatting so we can see exactly what is going on.

I apologize, but I cannot seem to figure out how to use the 6th icon </> to get my script to use the same indentations as there are in my Notepad ++. The indentations in my Notepad ++ are exactly the same as the ones in ex18.py on page 60 of “Learn Python 3 the Hard Way”.

When I click on the 6th icon (</>) , I see this statement:
indent preformatted text by 4 spaces (indent preformatted text by 4 spaces)

but I do not see any clicks (’) until I hold down simultaneously the CTRL+SHIFT+C keys. When I copy my Notepad ++ script between those clicks, it unfortunately still come out unindented.

I finally got exercise 18 to work properly on my PowerShell, but I had to use my Notepad text editor instead of my Notepad ++ text editor. For some reason, Notepad ++ does not indent the lines in the actual file (ex18.py) as I actually did indent them when I typed the file (ex18.py) into the Notepad ++ text editor. However, when I typed the same file (I called it “ex18a.py” to avoid confusion) into my Notepad text editor, my Notepad text editor evidently did actually indent the lines as I did indent them when I typed that file (ex18a.py) into my Notepad text editor. I am new to Notepad ++ (heck, I am new to all of this), and so I just assumed that Notepad ++ would indent the lines as I indented them when I typed them.

I just now got exercise 18 to work properly in my PowerShell from the Notepad ++ text editor file (ex18.py) by copying the Notepad text editor file (ex18a.py) and then pasting it into the Notepad ++ text editor. That became my new version of ex18.py, and it evidently had the correct indentations encoded within it because it ran properly and produced the correct results.

1 Like

Yes, Bryan,
I figured the indentation was the problem. Glad you solved it.

Hey Bryan,

So first up, when you post code you can do this:

# your python here

You can also edit your post rather than doing a reply. So hit the “kabab” (…) icon, then there’s a pencil. Click the pencil and edit it as needed until it looks good.

You can also use three backticks (then then then) if you can’t use [code] [/code].

Now, for the tabs problem

Go into Notepad++ and see in the options if it is using Tabs or spaces (or even worse, both). Make it use spaces only, 4 spaces per “tab stop”. Then see if you can have it turn on “visual spacing” or an option that makes it show you the space vs. tab characters. If you can’t find it then bug me and I’ll do a video showing it.

Once you do that you should be able to see the rogue tabs and remove them.

Finally, I delete you posts where you were just replying with your code again to fix it. Remember, you can always edit your posts (but if not then bug me again and I’ll do a video).

In Notepad ++, I could not find the “options” that you mentioned, but I did find:

Settings > Preferences > MISC > Auto-Indent

When I unchecked the box for “Auto-Indent” and ran the following Python code on my PowerShell:

def print_two(*args):
    arg1, arg2 = args
    print(f"arg1: {arg1}, arg2: {arg2}")

print_two("Zed", "Shaw")

My PowerShell screen displayed the correct results:
arg1: Zed, arg2: Shaw

Also, I have gone to informit.com/title/9780134692883 to view the instructional videos that are mentioned on page XVII of “Learn Python 3 the Hard Way”, but I can only find the O’Reilly Safari service that charges to view these videos. Is there no way to view your videos for free? Occasionally I can find on Google an instructional video for one of the exercises in “Learn Python 3 the Hard Way”, but they are evidently not done by Zed Shaw.

Bryan, join Zed’s course.
It includes the videos, weekly seminars plus you get to show Zed your code and get corrected.
Only the books are free, the rest of the materials are included in the course.
You can find Zed’s course here: https://shop.learncodethehardway.org/access/buy/14/
L.E. And I am not sure about the books.
But I am sure you’ll like it and the videos explain everything so well!

If you bought the book from me, InformIT, or Amazon (or anywhere), then you can go to informit.com and register your book to view the videos. If you haven’t purchased the book then, sorry, there’s nowhere to view the videos for free.