I don't see the difference

**Question:**Hi I am a beginner in Python coding and I am using Replit to type code and find out if it works when I run it. I work with ChatGpt to give me tasks. For a task I got I made code and got a result that wasn’t what it should be (snippet 1). I asked ChatGpt to check the code and got back that it should be okay. After asking it to provide me the code (snippet 2) it would write, I copied the answer and pasted it in Replit, and weirdly it gave the right result, but on the eye there is no difference in the code. To work them both at the same time I added ‘1’ to the GPT code not to give Replit double functions and calls, and it yielded the below snippets as result. Below the 2 code snippets that yield different results.
Can someone explain why I get different results? (apart from the ‘1’ I placed there on purpose)
Here are the results of the code, the code is below.

  • Snippet 1 yields:
    Name: Alice, Age: 25, gender: female
    Name: Alice, Age: 25, gender: female
    Name: Alice, Age: 25, gender: female, major: Algebra

  • While snippet 2 yields:
    Name1: Alice, Age1: 25
    Name1: Alice, Age1: 25, gender1: female
    Name1: Alice, Age1: 25, gender1: female, major1: Algebra

Repl link:

Code snippet 1:
def print_info(name, age, **kwargs):
  info = f"Name: {name}, Age: {age}"
  for key, value in kwargs.items():
    info += f", {key}: {value}"
    print(info)

print_info(name = 'Alice',age = 25)
print_info(name = 'Alice',age = 25, gender = 'female')
print_info(name = 'Alice',age = 25, gender = 'female', major = 'Algebra')

Code Snippet 2:
def print_info1(name1, age1, **kwargs1):
  info1 = f"Name1: {name1}, Age1: {age1}"
  for key, value in kwargs1.items():
    info1 += f", {key}: {value}"
  print(info1)

print_info1(name1='Alice', age1=25)
print_info1(name1='Alice', age1=25, gender1='female')
print_info1(name1='Alice', age1=25, gender1='female', major1='Algebra')

In the code that ChatGPT generated, in the function print_info (1 removed), the output of the variable info (1 also removed) occurs outside the for loop, which means that for one execution of the function, the output will occur in any case (even if no additional parameters are specified) and only one once.

In the code that you wrote, in the print_info function, the output of the info variable occurs inside the for loop, which means that during one execution of the function, the output will occur as many times as additional parameters are specified (that is, the output may occur, or may occur several times).

Because of all of the above, there is a difference in the output of the function.

4 Likes

Hi KAlexK, Thanks for the help. I was so stuck on seeing the same code that I overlooked this indentation.
This is probably also the reason that when I comment out call 2 and 3 in my code I get no result.

1 Like

You have drawn the right conclusions.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.