+ 4

Functions

How to execute functions without storing anything in a variable?

10th Aug 2024, 2:25 PM
Jakob
Jakob - avatar
12 odpowiedzi
+ 3
I think that's an error, you define a function named 'fruit' and you also create a list named fruit. The compiler will be confused between the two because you want to print the list at the same time you also need to make sense of function, and you cant do that if both of them have same name. Your list has no quotation marks, so that's also an error. fruits = ["apple," pear"] def basket(): print(fruits) basket() You can also think function as "containers" since its reusable, so I rename the function basket. To call a function, you have to call it just like I did at the end of this code.
11th Aug 2024, 4:02 AM
Joseph Sintos
Joseph Sintos - avatar
+ 4
can you give an example of a code snippet that you want to rewrite? there's nothing wrong with using a variable
10th Aug 2024, 2:44 PM
Lisa
Lisa - avatar
+ 1
fruits is a list, not a function remember to use the "" for strings: fruits = ["apple", "banana"] print(fruits)
10th Aug 2024, 3:54 PM
Lisa
Lisa - avatar
+ 1
print(fruits) we do not need a function what should your function do? what should the output look like?
10th Aug 2024, 4:22 PM
Lisa
Lisa - avatar
+ 1
Thank you! Now it's working
11th Aug 2024, 7:27 AM
Jakob
Jakob - avatar
0
fruits = [apple, pear] def fruits(): print(fruits)
10th Aug 2024, 3:31 PM
Jakob
Jakob - avatar
0
How can I execute that
10th Aug 2024, 3:32 PM
Jakob
Jakob - avatar
0
If fruits would be the Name of the function I mean
10th Aug 2024, 4:03 PM
Jakob
Jakob - avatar
0
and what is fruits = [...] in your example?
10th Aug 2024, 4:19 PM
Lisa
Lisa - avatar
0
The variable for print
10th Aug 2024, 4:20 PM
Jakob
Jakob - avatar
0
It was only an example
10th Aug 2024, 5:08 PM
Jakob
Jakob - avatar
0
-> what should the output look like?
10th Aug 2024, 5:59 PM
Lisa
Lisa - avatar