**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')