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.
Related
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()
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 2 years ago.
Improve this question
i suspect that this is a simple answer, but i cannot figure out the answer. (I did give it due diligence)
i wrote a simple python program to identify prime numbers. the program is function, but i'm receiving strange results in the output. when i have it write a number with multiple digits, each number is comma separated; for example, 13 is added to the document as 1,3. I would like to have a comma after each full number (13,) and don't want commas within the number (1,3 or 1,301). eventually, i want to have each number on its own row (one of the issue that i ran into in my g1 program is that the row became too long around 50mill ;-)
Any thoughts?
#!/bin/python3
import time
import os
import csv
folderLocation = "c:/notNow/"
primeName = "primeNumbers.csv"
# notPrimeName = "noPrimeNumbers.csv"
primePath=folderLocation + primeName
# notPrimePath=folderLocation + notPrimeName
no=13
os.makedirs(folderLocation)
f = open(primePath, "w")
writer = csv.writer(f)
writer.writerow(str(no))
output: 1,3
writerow expects a sequence of items (e.g list). A string is just seen as a sequence of individual characters, try this instead:
writer.writerow([no])
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 3 years ago.
Improve this question
I was working on a problem and came across a syntax error for a print statement. I started commenting out lines and have an EOF error for a comment line. Any clue how to fix this EOF error for comments?
I've tried commenting out the lines, deleting the lines and typing them again in case I had a typo, and looking up similar solutions
Here is what I am currently trying to run:
# np_baseball is available
# Import numpy
import numpy as n
# Create np_height_in from np_baseball
np_height_in = np_baseball[0]
print(n.median(np_height_in))
# Print out the mean of np_height_in
print(n.mean(np_height_in)
#print(n.median(np_height_in))
# Print out the median of np_height_in
Here's the current error:
File "", line 12
# Print out the median of np_height_in
^
SyntaxError: unexpected EOF while parsing
print(n.mean(np_height_in)
is missing a closing ) . It should be:
print(n.mean(np_height_in))
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.
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 5 years ago.
Improve this question
Acceleration in a given point is given by a=5-0.004364*v^2
I want to store all the values of v and x and a in lists so that I later can plot them as a function of time. Keep in mind I'm a beginner. This is my code so far:
x_val=0
x=[]
x.append(x_val)
v_val=0
v=[]
v.append(v_val)
a_val=5.0
a=[]
a.append(a_val)
h=0.1
while x_val <=100:
v_val += (a_val*h)
x_val += (v_val*h)
a_val=(5-0.004364*(v_val**2)
a.append(a_val)
v.append(v_val)
x.append(x_val)
I'm getting a syntax error on "a.append(a_val)": invalid syntax
What am I doing wrong here? Please help
There is closed parenthesis missing in the below line
`a_val=(5-0.004364*(v_val**2)`