Function returning nothing [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am creating a desktop application with tkinter that utilizes OpenCV for some tasks later on. I am trying to create a function that reads a file path and returns that path as a string. It returns <function UploadAction at 0x00000246B6A42E50> and I have a print() to verify if askopenfilename is printing the path correctly and it is. So what am I overlooking/not looking at here? Here's the code:
def UploadAction():
file = filedialog.askopenfilename()
print(file)
return file
mystr= UploadAction
img= cv.imread('{}'.format(mystr))
def Image(event=None):
#cv.imshow('img', img)
#cv.waitKey(0)
#cv.destroyAllWindows()
print(mystr)
print(UploadAction)
The functions are called through buttons, they work fine. Thanks in advance.

You need Parenthesis(), or else the function won't be called -
mystr= UploadAction()
Also, this - img= cv.imread('{}'.format(mystr)) can be simplified -
img= cv.imread(mystr)

You need parenthesis in order to call "UploadAction".
If you don't put parenthesis, you will store a pointer to the function rather than executing UploadAction and setting the return value into mystr.
mystr = UploadAction()

Related

I keep getting errors with an "if var.startswith" statement [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I'm trying to make it so that I can input someone into console and have it set a variable to it, and every time with the if statement it gives the error
File "main.py", line 58, in <module>
if meInput.startswith("%send"):
AttributeError: 'builtin_function_or_method' object has no attribute 'startswith'
Here's the code:
if input.startswith("%send"):
myinput = input.split(" ", 2)[2]
channel = client.get_channel(12324234183172)
I've tried putting it into a variable such as variable = input then changing the if statement to match the variable, but it does the same thing.
Read the error message carefully! It is telling you that input is not a string, but a function — a function that would return a string if you called it, but you didn’t. Try this instead:
if input().startswith("%send"):
Note the parentheses. That is how you call a function in Python, and in most other languages.

Python .upper and .lower methods returning no results [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm having trouble getting Python's .upper and .lower methods to return anything. Here's the code:
initials = input("Enter your initials: ")
uppercase = initials.upper
print(uppercase)
What it returns is:
Enter your initials: mj
<built-in method upper of str object at 0x7f50734b01f0>
I originally integrated this into a larger function, but when I call uppercase later in the function the variable remains empty. I'm working in Google Colab.
To call a method in Python, you must open and close parenthesis after the name of the method:
uppercase = initials.upper()
See the example in the documentations of str.upper.
Here you go, just make sure you use .upper() method appropriately with your variable. :)
initials = input("Enter your initials: ")
print(initials.upper())

Having issues writing a new data file [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to write a Python program that writes out 20 steps between 0 and 2*pi, with the value and the sin and cos of that value.
I have:
import math
f=open('question10.txt','w')
x=0
pi=math.pi
f.write("x, y=sin(x), z=cos(x)\n")
while x<=2*pi:
f.write("{}, {}, {}\n".format(x, math.sin(x), math.cos(x)))
x = x+(pi/10)
f.close()
I have no idea what isn't working. It won't even create the data file, and isn't giving me any sort of error.
One thing you can try is to use with instead of writing and closing directly. This will ensure that the file will be closed after writing without you having to manage it:
import math
x=0
pi=math.pi
with open("question10.txt", "w") as out_file:
out_file.write("x, y=sin(x), z=cos(x)\n")
while x<=2*pi:
out_file.write("{}, {}, {}\n".format(x, math.sin(x), math.cos(x)))
x = x+(pi/10)
If you are still having problems, perhaps you can explain the error you are encountering?
There's nothing wrong with your code. You should check that you run your python file correctly.

Why will this function not open my file? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
def open_file(filename):
file_open= open(filename,"r")
return file_open
When I try and call the function I get the following results:
>>> open_file(random.txt)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
open_file(random.txt)
NameError: name 'random' is not defined
try
open_file('random.txt')
Strings in Python need to be quoted.
random is being interpreted as an object, and is undefined.
You forgot quotes:
open_file('random.txt')
python thinks random is an object, which obviously you didn't define. The quotes make it a string.
you just need to input the filename as a string; here's how it must be done:
>>> open_file('random.txt')
note that your function works just fine, all you need to do is call it properly.

broken print feature in python 2.7.11 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
A problem exists in Python 2.7.11, with the print function:
elif e=="randomize w and x":
random=randint(int(w),int(x))
print random
elif e=="randomize w and y":
random=randint(int(w,int(y))
print random
The boldfaced print shows up as a syntax error, yet all 278 others in my program do not. Why this is, and how I fix it?
The problem is that in
random=randint(int(w,int(y))
a close parenthesis after w is missing, therefore Python thinks the expression continues on next line, but print at that point is a syntax error.
Your problem is not with the print statement, rather the line right before it. The line before hass inbalanced parenthesis:
random=randint(int(w,int(y))
Make sure you balance them out (add an extra ) at the end), and your error on the next line will disappear.

Categories