How do I shuffle the print statement of 3 separate functions? [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
def a():
print(a)
def b():
print(b)
def c():
print(c)
How do I shuffle a, b and c? I cannot print a,b,c out of the function because there is additional code that I haven't mentioned here in order to simplify the question. I've tried putting all 3 inside random.shuffle() and also tried to associate variables with each function and tried to shuffle the variables (this also doesn't work out because of the code inside each function).
Send help.

The issue you are most likely running into is that you calling the functions inside of your shuffle. Just put the functions themselves without calling them and you can easily do this:
random.shuffle([a,b,c]) # correct
# vs.
random.shuffle([a(),b(),c()]) # incorrect, calls functions before shuffle
Then you can do a for loop and call them or whatever you need:
for f in random.shuffle([a,b,c]):
f()

Related

What are functions, and how are they used [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 22 days ago.
Improve this question
I am a beginner and need to know what a function for a small tic tac toe project. But all I need to know is this as understood the rest. is and how to use one as well as what can they do
I was just learning from the python docs about a function and could not understand, as I was searching on google still, I could not find anything, if one could help, it would help me loads.
This is the code for my tic tac toe that the YouTuber gave as an example:
def cool(a,b):
return(a+b)
print(cool)
Thanks,
Gops
Welcome to the world of programming.
As a beginner, you should imagine functions as "reusable" part of your code, with the feature of having temporal variable that can change the behavior of the function based on the body of the function.
Calling the functions
To reuse that part of your code that you wrote in your function, you have to call it.
def python_cool():
print("Python is cool")
python_cool()
python_cool()
python_cool()
Technically, this code is the same as:
print("Python is cool")
print("Python is cool")
print("Python is cool")
At this point, if you we are talking about parameters, we have to talk about arguments too.
Parameters
Parameters are the one that you are declaring your function with:
def sample_function( paramter1, parameter2, paramterN ):
Arguments
Arguments on the other hand, are the values when you are calling the function with.
sample_function(argument1, argument2, argumenN)
Example
def print_values( value_1, value_2) :
print("The first value is: ", value_1)
print("The second value is: ", value_2)
print_values(1, 2)
print_value(True, False)
print_values("apple", "peach")
Return values
Another great benefits of functions that they are able to "become" a value that you can store in a variable in the future
For example:
def multiply_by_three( number ) :
return number * 3
This function has a return value of an integer which can be used as:
sample_variable = multiply_by_three(5)
which is technically:
sample_variable = 15
In Python, a function is a block of instructions or code that is used to perform an action. Functions provide an organized way to structure code, making it easier on the human eye. It seems as if you are tired to add a and b, however, a and b are not given values in the return statement.
I self-learned python and would look at the W3Schools stuff which is quite good.
A function is, in effect, a way of running a section of code with a keyword, if you had
def cool(a,b)
print(a+b)
cool(1, 3)
You would have the function running the code you have put inside it and so printing the value of a+b (4) using the values a and b which were passed into the function.
The code you posted would not work because to run the function, a and b need to be passed, which is not done in the question.

Return value in Python for Loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi want to return value after for loop. My code is -
def asd():
data.append("a")
data.append("b")
for i in range(0,2):
value = data[i]
return value
I am expecting to return both a and b when calling the function but it is returning only b. Is there any other method. Thanks in advance
Unless you have a very pressing reason for doing things this way, this example can be simplified to
def asd():
return 'a', 'b'
This is not an answer, but it will help you to find your mistake.
The = is an assignment operator. The value provided behind the = is assigned to the variable in front of it, e.g. a = 3 means that a now carries the value 3.
Now two easy questions
a=3
a=5
print a
Which value gets printed?
Similarly,
for i in range(100):
a = i
print a
What value gets printed?

Variables defined inside if or for statements [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
What happens to variables assigned for the first time (defined) inside if statement or for loop if long time passed from when their code run. Is there some sort of garbage collection that may result in a not defined variable exception. For example:
if True:
a=1
else:
a=3
# long time passed and other codes run
# .
# .
# .
print (a)
I encountered an error in my code that I suspect this to be the reason. Is it documented somewhere in official Python documentation ?
In Python, if you define a variable within an if statement, it will continue to exist after the if statement concludes. Scopes are defined for a class, a def, or the global scope; if you're in a function and you define a variable inside if, for example, that variable will exist until the function is done executing.
Be careful, however, of defining variables in code like this:
if x == True:
a = 1
else:
print "Not true"
If you have code like this, and x ends up being False, then a will not be defined. Later calls to a will throw an exception as a result. So make sure that you get rid of any potential problems of that sort.

Hand over global variables to methods in python? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
This is more a question about good programming style. I usually work with Java, and now I do some working with Python. In Python, there is no need to hand over global variables if you only want to read from them. On the other hand, I think the Java syntax is more helpful in his regard. You have to hand over required variables, and so you can see what variables are used by what method, which I am sure is helpful for somebody who is reading your code.
Now do you hand over variables in Python although you could already access them because they're global? What is the good 'pythonic' way?
Thanks,
ZerO
def foo(a):
a = 2
foo(1)
1 is 'handed over' to method foo().
Yes, this
def foo(a):
a = 2
foo(1)
is preferred over this
a = 1
def foo():
a = 2
foo()
Imagine you have 3 methods that all do something to a list.
a_list_name = []
def a()
a_list_name.something
def b()
a_list_name.something
def c()
a_list_name.something
a()
b()
c()
If you define the list 'global' you will have to refer that exact list in each method. If you for some reason want to change the list name you now have to edit all 3 methods.
However if you pass in the list through a parameter you only have to edit the method calls and the method code can remain untouched. Like this
def a(l)
l.something
def b(l)
l.something
def c(l)
l.something
my_list = []
a(my_list)
b(my_list)
c(my_list)
This makes your code more modular, and most of all it makes your code (methods) testable because they don't depend on some variable that is defined somewhere else

Why are decorators useful? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
So I read this page about decorators, but I still don't understand when decorators are useful.
Consider a piece of code defining a function f, and then calling it multiple times. For some reason we want f to do some extra work. So we have 2 ways to do it:
define a new function g which calls f, and does the extra work needed. Then in the main code, replace all calls to f by calls to g
define a decorator g, and edit the code to add #g before calls to f
In the end, they both achieve the same result and the advantage of 2) over 1) is not obvious to me. What am i missing?
Suppose you have a lot of functions f1, f2, f3, ... and you want a regular way to make the same change to all of them to do the same extra work.
That's what you're missing and it's why decorators are useful. That is to say, functions that take a function and return a modified version of it.
The decorator # syntax is "just" for convenience. It lets you decorate the function as it is defined:
#decorated
def foo():
# several lines
instead of somewhere after the function definition:
def foo():
# several lines
foo = decorated(foo)
In fact of course the latter code is pretty horrible, since it means that by looking at the first definition of foo in the source, you don't see the same foo that users will call. So without the syntax, decorators wouldn't be so valuable because you'd pretty much always end up using different names for the decorated and undecorated functions.

Categories

Resources