From the course Ai python for beginners, I still found it very hard to pass the exercise Pluto’s poetic journey, where I was ask to use return statement, any help please
@Emmysam though what you are doing makes tons of sense-- The instructions do not express what extension the file has. Perhaps that is the problem (?).
You should be posting the the relevant specialization, and you should avoid to post solutions publicly even if they are wrong!
Well, in your function, you have to return the contents variable, which you have assigned to something within the function, not calling the function at its return!
Hi @Emmysam,
Can you elaborate the issue you are facing and in which exercise ? Without sharing your solution code please.
Thanks,
Mubsi
Hello!
When you use return
in a function, it sends a value back to where the function was called. This allows you to save, reuse, or further process the result.
Example 1: Returning a Calculation Result
def add_numbers(a, b):
return a + b # Sends the result back to the caller
result = add_numbers(3, 5) # Stores the result of the function
print(result) # Output: 8
- The function
add_numbers
calculates the sum ofa
andb
. - The
return
statement sends the result back to the caller (line 4). - The result is saved in the variable
result
. - You can now use
result
in other parts of your code.
Example 2: Using Returned Values in Other Functions
def square(x):
return x * x # Returns the square of x
def double_square(x):
result = square(x) # Uses the returned value from square()
return result * 2 # Doubles the squared value
print(double_square(4)) # Output: 32
- The
square
function calculates x2x^2x2 and returns it. - The
double_square
function uses the returned value fromsquare
and doubles it. - The final result is printed.
Example 3: Without return
, It’s no value (None)
def greet(name):
print(f"Hello, {name}!") # Only displays the greeting
greeting = greet("Pluto") # No `return` here
print(greeting) # Output: Hello, Pluto! \n None
- Since
greet
does not usereturn
, it doesn’t send any value back. - By default, Python assigns
None
when there’s noreturn
.
PS: Also make sure to sort the code in the grade, I just left a space at the end of a cell and the grader gave me an error for that, I removed the space and it gave me a good score.
C1M3_Assignment, under Pluto poetic journey, my problem is, firstly the file they are talking about, what exact file , is it file.txt?, and how will I organized the code, I needed help urgently, cus d deadline is 20th January
Hi @Emmysam,
The markdown (text in the assignment) and code cell comments explain and cover everything. Please read those.
Best,
Mubsi