My code looks like:
def g_b():
items_in_bag = []
done=False
bugout_bag = 'Bug Out Bag'
while done == False:
item = input('What bags do you have? [Enter x to stop]')
items_in_bag.append(item)
if item == 'x':
done = True
items_in_bag.remove('x')
break
else:
continue
items_in_bag.append(bugout_bag)
print("Your bags\n")
print(items_in_bag)
return items_in_bag
def g_c():
coins_in_bag = []
done=False
while done == False:
coin_item = input('What coins do you have? [Enter x to stop]')
if coin_item == 'x':
done = True
break
else:
coins_in_bag.append(coin_item)
continue
print("Your coins\n")
print(coins_in_bag)
return coins_in_bag
def a_c_t_b(items_in_bag, coins_in_bag):
#print('Here are your coins:\n')
#g_c()
#print('Here are your bags:\n')
#print(items_in_bag)
print (items_in_bag,coins_in_bag)
return (items_in_bag,coins_in_bag)
def main():
g_b()
g_c()
a_c_t_b(items_in_bag,coins_in_bag)
main()
However, when i run this code like: import myfile
It gives me an error of:
File ".\myfile.py", line 51, i
a_c_t_b(items_in_bag,coins_in_bag)
NameError: global name 'items_in_bag' is not defined
I'm simply trying to return the values of items_in_bag,coins_in_bag from their respective functions.
Thank you
Please call your functions more sensible names.
To answer your question, your g_b and g_c functions return values, they don't return names. At the point where you call a_c_t_b, Python has no idea what items_in_bag is, because yo'uve never defined it. Python can't know you mean "the value returned from g_b": you have to tell it.
items_in_bag = g_b()
coins_in_bag = g_c()
a_c_t_b(items_in_bag, coins_in_bag)
You are calling g_b and g_c but never catching their returned values.
You can either do:
def main():
items_in_bag = g_b()
coins_in_bag = g_c()
a_c_t_b(items_in_bag, coins_in_bag)
or:
def main():
a_c_t_b(g_b(), g_c())
When you import the module main function is executed (call in last line). And main function use undefined identifiers items_in_bag and coins_in_bag:
def main():
g_b()
g_c()
a_c_t_b(items_in_bag,coins_in_bag)
Probably you want something like
def main():
items_in_bag = g_b()
coins_in_bag = g_c()
a_c_t_b(items_in_bag,coins_in_bag)
Related
What's wrong with this that ain't working. I have to ask user for command and if command from setAction() is = 'temp' i have to just do delete() but that's not working.
import glob
import os
def setAction(action):
action = input('Mt Manager> ')
def delete():
pattern = r"C:/Users/kpola/AppData/Local/Temp/**/*"
for item in glob.iglob(pattern, recursive=True):
# delete file
print("Deleting:", item)
os.remove(item)
while True:
if setAction('temp'):
delete()
print('Hello world')
else:
quit()
In the function setAction you are only assinging action to temp over and over again. And since setAction has no return statement it will always return None
def setAction(action):
return input('Mt Manager> ') == action
This snipped should return if your input is equal to "temp"
i use make_id to make id think but i got error: TypeError 'type' object is not iterable
i dont no why i got this error,can any person help me?
i am Chinese(Taiwan)
my code:
from msilib import make_id
from tkinter import simpledialog,Tk
def number():
win = make_id('')
def winner_number():
numberd = 'c4'
def get_task():
task=simpledialog.askstring("id製造器",'你要製造id嗎?')
return task
def get_message():
message=simpledialog.askstring('你的id是:',number,'中獎號碼:',winner_number)
return message
screen = Tk()
while True:
task=get_task()
if task == "要":
massage= get_message
number = make_id
if number(str) in winner_number():
print('你的運氣超好!!')
elif task=='cancel':
print('你失敗了!!')
else:
break
the code is have multiple errors but let me intrudes few of them
1 how functions values work in python
def function(values)
print(values)
function(10)
# 2nd way
def functions(values)
return values
a = function(10)
print(function(10))
# will give same result
so basically you are trying to do is just run stuff in function
def numebr():
win = make_id('')
def winner_number():
numberd = 'c4'
but you are comparing two function..
instade you could do
def number():
return make_id('')
def winner_number():
return 'c4'
if number() == winner_number():
print('you win')
So a problem I'm facing is this:
I defined 2 functions and one function uses the variable of the other function
Now when I run both of them using the following code, it works properly:
def type_anything():
use = input(">")
return use
def print_it():
print(use)
if __name__ == '__main__':
while True:
use = type_anything()
print_it()
Output:
> abcd
abcd
> efgh
efgh
> anything
anything
But when I decide to make a main function that will run both the above functions and then run the main function under the "if __name__ == ......" line, something like this:
def type_anything():
use = input("> ")
return use
def print_it():
print(use)
def run_it():
while True:
use = type_anything()
print_it()
if __name__ == '__main__':
run_it()
The program doesn't run properly instead shows this error:
> anything
Traceback (most recent call last):
File "C:/<location>/sample.py", line 17, in <module>
run_it()
File "C:/<location>/sample.py", line 13, in run_it
print_it()
File "C:/<location>/sample.py", line 7, in print_it
print(use)
NameError: name 'use' is not defined
Why is this happening? What do I need to do?
This should solve your problem:
def type_anything():
use = input("> ")
return use
def print_it(use):
print(use)
def run_it():
while True:
use = type_anything()
print_it(use)
if __name__ == '__main__':
run_it()
The print_it function is not aware of any variable use hence the error.
Noticed how the type_anything function returns the variable use and the print_it function accepts an argument and then prints that.
Please do not get into the habit of using global variables, in the long run these will break everything you write. This is just an example to help you with your understanding of the problem!
Your problem is variable scope. In the first example your variable use is global because you define it in the main program:
if __name__ == '__main__':
while True:
use = type_anything()
print_it()
It is not defined in type_anything, you could rename this variable to whatever and it would still work:
def type_anything():
x = input(">")
return x
In the second example you define it in a function:
def run_it():
while True:
use = type_anything()
print_it()
You could fix this by making use a global variable:
def run_it():
global use
while True:
use = type_anything()
print_it()
A much better way of doing this
Pass the variable use to the function that uses it:
def print_it(use):
print(use)
def run_it():
while True:
use = type_anything()
print_it(use)
You can't use a variable defined in one function in another function.
Each function needs to receive the arguments it uses.
def type_anything():
use = input(">")
return use
def print_it(use):
print(use)
if __name__ == '__main__':
while True:
use = type_anything()
print_it(use)
I would like to know how to call one global variable with two different values in a class and call them in the other class (within which behave such as flags).
in SerialP.py
Class SerialP(object):
def ReceiveFrame (self, data, length):
global myvariable
if x:
myvariable = 1:
elif y:
myvariable = 2
in fmMain.py
Class fmMain:
def OnReadConfig(self, event):
if SerialP.myvariable = 1:
#do this task
if SerialP.myvariable = 2:
#do another task
There are a few issues with your code.
First, comparison is done with == and not with = which is used for assignment. Also, you have not included the import statement which might be misleading.
In fmMain.py
import SerialP # and not from SerialP import SerialP
Class fmMain:
def OnReadConfig(self, event):
if SerialP.myvariable == 1: # changed to ==
#do this task
if SerialP.myvariable == 2: # changed to ==
#do another task
I am currently looking at trying to use a callback in Python.
What I would like to do is return a value from the callback and then use this return value for conditional processing. So for example if the user enters "Y" I would like to print something to the console.
As I am new to Python the code below is as close as I can get currently but:
a) I am not sure if it is the Pythonic way of doing it
b) the correct way of doing it?
class Observable:
def subscribe(self,callback):
self.callback = callback
def fire(self):
self.callback()
class CallBackStuff:
def doCallback(self):
userInput = raw_input("Please enter Y or N?")
return userInput
if __name__ == '__main__':
s = CallBackStuff()
o = Observable()
o.subscribe(s.doCallback)
t = o.fire()
print t
The easiest way I can think of to do this in your code is to just store the input as
a variable in the CallBackStuff object. Then after you've called the call-back function, you can just access the input data from the CallBackStuff instance. Example below.
class Observable:
def subscribe(self,callback):
self.callback = callback
def fire(self):
self.callback()
class CallBackStuff:
storedInput = None # Class member to store the input
def doCallback(self):
self.storedInput = raw_input("Please enter Y or N?")
if __name__ == '__main__':
s = CallBackStuff()
o = Observable()
o.subscribe(s.doCallback)
o.fire()
print s.storedInput # Print stored input from call-back object
class Observable(object):
def __call__(self, fun):
return fun()
class Callback(object):
def docallback(self):
inp = raw_input()
return inp
if __name__ == "__main__":
print Observable()(Callback().docallback)