A
A
AndrewMu2020-06-26 22:09:28
Python
AndrewMu, 2020-06-26 22:09:28

How to invert values ​​in python?

How do I invert a variable like variable x?

x=o
x = not x
print(x)

works, but when I create a function, it doesn't
x=True
def ain():
    x = not x
    print(x)
ain()

Most likely a problem with global and local variables.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2020-06-26
@bacon

1. Pass a variable to the function
2. Return the result from the function
What could be simpler? Well, forget about the use of global variables, beginners do not know how to use them.

A
Alan Gibizov, 2020-06-26
@phaggi

For example, like this:

x = True
def ain(_x):
    _x = not _x
    print(_x)
    return _x
x  = ain(x)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question