python variable assignment, can i disable var? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
error:
UnboundLocalError: local variable 'x' referenced before assignment
x is not defined yet...
can i disable x in buttonAritmethic and only enable when I call him?
buttonAritmethic = (x*imgfW)+buttonCenter, 350
for x in range(buttonReach):
self.canvasbackground = self.canvasFrame.create_image(buttonAritmethic, anchor = "nw", image = self.imgf)

Why don't you make it into a function?
def buttonAritmethic(x):
return (x*imgfW)+buttonCenter, 350

Related

(Python) Questions about functions and arguments [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
I have code:
def users_list_reaction(users_dict, prefix_dictionary, event):
login = event.widget.get()
prefix_str = users_dict.get(f'{login}')
for i in prefix_str[0]:
if i == ' ':
pass
else:
for x in i:
prefix_dictionary[x].select()
But I only need to pass two of the three arguments. I have error TypeError: users_list_reaction() missing 1 required keyword-only argument: 'event'. None how answer will not help, because the event is a tkinter library widget and it is needed to get information from the combobox. I don't want use global users_dict . What to do?
I tried to search for information on the Internet, but I don’t understand how to formulate it correctly, because I didn’t find an answer to mine

How to a Python custom function to raised value with inner function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
This is the current code, tried to print but it return None..can some1 enlightened me how to do it?
def calculate(Amount1, Amount2):
def inner(square,cube):
square=Amount1**2
cube=Amount2**3
return (inner(Amount1,Amount2))
print(calculate(2,4))
Expected result (4,64)
def calculate(Amount1, Amount2):
def inner(square,cube):
square=Amount1**2
cube=Amount2**3
return((square, cube))
return (inner(Amount1,Amount2))
print(calculate(2,4))
you forgot to return the values from your inner function

Python: Passing argument from top-level function to embedded function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
This is a simplified example. But basically, I have function within a function. I want to pass the argument from the top-level function to the embedded function, but it doesn't work. How do I solve this?
Thank you
def Check(file_name, mh_criticalval):
test_file=file_name
def eval_value(excel_sht, excel_col, mh_value):
if excel_sht.cell(row=i, column=excel_col) > mh_value:
cell_cny_b=excel_sht.cell(
row=i,
column=excel_col
).value='OK'
eval_value(sht, 9, mh_criticalval)
Check('test.xlsm',1)
Method 1: De-Nesting
The most obvious method here is to remove the nested functions. Doing this is simple:
def eval_mh(excel_sht, excel_col, mh_value):
if excel_sht.cell(row=i, column=excel_col)>mh_value:
cell_cny_b=excel_sht.cell(row=i, column=excel_col).value='OK'
def CheckDiag(file_name, mh_criticalval):
test_file=file_name
eval_mh(sht, 9, mh_criticalval)
CheckDiag('test.xlsm',1)
All we did here was move the inner function out. However, if you still wanted them to be nested, try the following.
Method 2: Local/Global Variable Exploitation
Here, we replace all instances of mh_value with mh_criticalval
def CheckDiag(file_name, mh_criticalval):
test_file=file_name
def eval_mh(excel_sht, excel_col):
if excel_sht.cell(row=i, column=excel_col)>mh_criticalval:
cell_cny_b=excel_sht.cell(row=i, column=excel_col).value='OK'
eval_mh(sht, 9)
CheckDiag('test.xlsm',1)
Method 3: Why 2 functions?
In this method, we remove the function entirely!
def CheckDiag(file_name, mh_criticalval):
test_file=file_name
if sht.cell(row=i, column=9)>mh_criticalval:
cell_cny_b=sht.cell(row=i, column=9).value='OK'
eval_mh(sht, 9)
CheckDiag('test.xlsm',1)

Define a function one() that prints the string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Define a function one() that prints the string "Question 1". Do NOT call the function.
and the result should be
TEST : one()
Result: Question 1
I have tried this code
def one():
print ("Question 1")
but nothing happened
I believe you just need to remove the space after "print" to get what you are trying to do, but as #Brack mentions, you will still need to call the function:
def one():
print('Question 1')
Console Input:
one()
Output:
'Question 1'

Why my instance does not have a name [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The snippet of code is below. I want my object name to be equal to element[0]. My scripts successfully generates the objects, but fails to 'name them' I cannot understand why.
master_inventory = []
def import_catalogue():
with open("./catalogue.txt", "r") as raw_catalogue:
for page in raw_catalogue:
page = page.split('\r')
for line in page:
element = line.split('\t\t\t')
element[0] = Antique(element[0], element[1], element[2], element[3], element[4], element[5], element[6], element[7], element[8], element[9])
master_inventory.append(element[0])
import_catalogue()
print master_inventory[1]
>>> <__main__.Antique instance at 0x10980f320>
print master_inventory[1].sku
>>> A00001
You should implement the __str__ function in the Antique class, so you choose how you want to display your object's name. e.g.:
class Antique():
…
def __str__(self):
return "Antique({},{},{})".format(self.foo, self.bar, self.foobar)
…

Categories

Resources