Is this not an error?

I believe there is an error on this lesson. Code Academy provides me with the preloaded syntax of this first and last name:


first_name = “Reiko”
last_name = “Matsuki”

Then, asks me to create a function. However, an error message comes up at the bottom of the screen saying it expected the concatenation of a completely different name from the last exercise. See screenshot.

Be sure to use the parameters given in the instructions.

Your function has an error in it. The SCT for the lesson checks additional ‘test cases’ that aren’t included in the skeleton code provided. Your error is in this line:

password = fname[-3:] + lname[-3]

You need the last 3 letters of each name.

1 Like

There is DEFINITELY an error in this challenge

As you can see I’m using both first and last name but when I run it it does not only use different names than given in code but also uses only last name.

Also when I tried different route before I got this error

This far in Python course I know I can connect strings like that, this error should not occur so there’s something very wrong with this lesson. And yes, I tried casting im and naz to strings with str(), same result.

There’s no error with the lesson. The SCT for the lesson (Submission Correctness Test) tests your code behind the scenes using Julie Blevins. It would make more sense if the error message showed the error using Reiko Matsuki, but the fact remains that your code does not return the expected output.

You are correct that something like this should work:

a = "Hello, "
b = "World!"
c = a + b
print(c) #Hello, World!

The problem you have, is that pass is a keyword. It tells the computer to literally do nothing. You cannot assign a value to pass, hence the syntax error.
pass is generally used as a placeholder when building a program like so:

def some_function():
    pass #will build this function later

or to facilitate the control flow in some situations though I’m not a fan of this technique:

for x in range(50):
    if x % 2 == 0:
        pass
    else:
        print(x) #prints only odd numbers, but there many other ways to do this

Now let’s look at why the code in your first screenshot returned insins instead of lieins.

first_name = "Julie" #using Julie Blevins to illustrate what the SCT is checking
last_name = "Blevins"
def password_generator(first_name, last_name):
    im = first_name[len(first_name) - 3:]
    naz = im = last_name[len(last_name) - 3:]
    return im + naz    
    
print(password_generator(first_name, last_name))

Output:

insins

So, why is that? ins is the last 3 letters in the last name. We have it twice instead of lie and ins.
What is happening here?:

naz = im = last_name[len(last_name) - 3:]

How can we tell? Try printing?

first_name = "Julie" #using Julie Blevins to illustrate what the SCT is checking
last_name = "Blevins"
def password_generator(first_name, last_name):
    im = first_name[len(first_name) - 3:]
    naz = im = last_name[len(last_name) - 3:]
    print("im:", im)
    print("naz:", naz)
    return im + naz
        
print(password_generator(first_name, last_name))

Output:

im: ins
naz: ins
insins

Well, that doesn’t look right. im was supposed to be lie. Try adding another print()?

first_name = "Julie" #using Julie Blevins to illustrate what the SCT is checking
last_name = "Blevins"
def password_generator(first_name, last_name):
    im = first_name[len(first_name) - 3:]
    print("im:", im)
    naz = im = last_name[len(last_name) - 3:]
    print("im:", im)
    print("naz:", naz)
    return im + naz    
    
print(password_generator(first_name, last_name))

Output:

im: lie
im: ins
naz: ins
insins

Okay. Now we’re getting somewhere. The value of im changed between the two print() statements. Was that intended? No. What changes the value of a variable? An assignment (=). Does the code assign a value to im between the two print() statements?

    print("im:", im)
    naz = im = last_name[len(last_name) - 3:] #?
    print("im:", im)

Looks like the code assigns the last 3 letters of the last name to im, and then assigns the value just assigned to im to naz.

How can you change that to assign the intended value to naz?

1 Like

Ok, I didn’t know about the pass being functional so…I get the pass but naz = im? Maaaaaaan, I’m an idiot :smiley: I was copypasting a line and made a mistake. Im catching myself on stuff like this more than I would feel comfortable like this, is there some clever way to detect stupid mistakes like this one? Some handy tool? Or do I have to just learn to be more careful?

1 Like

The ‘clever’ way to find bugs, is to do as I did in my previous post. The output wasn’t what was expected, so I started with where the output came from, and added print() statements to see what was happening. You have to start at one end of your code, and work towards the other while making observations about what is happening. The print() statement is a good way to make observations. As you gain experience, you may be able to spot things with fewer print() statements, but the process will always be the same.