I have to make a calculator with argv.sys. When I run my code I keep getting this error:
>>> "C:\Users\admin\Desktop\uni\Informatik BW\assignment.py" + rect 0 0 10 10
File "<stdin>", line 1
"C:\Users\admin\Desktop\uni\Informatik BW\assignment.py" + rect 0 0 10 10
^
SyntaxError: invalid syntax
>>>
Here is my program:
import sys
import math
def area_rectangle(x,y,widht,height):
return (widht*height)
def xy_centroid_rectangle(x,y):
return (k + l * 0.5)
#def area_circle(x,y,r):
#return (r*r*math.pi)
#def xy_centroid_circle(k,r):
# return ((4 * r / 3 * math.pi) * 2)
#def area_half_circle(x,y,r):
# return (r * r * math.pi / 2)
#def xy_centroid_half_circle(k,r):
# return (4 * r / 3 * math.pi)
#def area_right_triangle(x,y,a,h):
# return (a * h / 2)
#def xy_centroid_right_triangle(k,l):
# return (a + h + math.sqrt((a * a) + (h * h)))
x = 0
y = 0
a = 0
fx = 0
fy = 0
f = 0
i = 1
while i < len(sys.argv):
vz = sys.argv[i]
print i
print vz
if sys.argv[i + 1] == "rect":
f = area_rectangle(float(sys.argv[i + 2]),float(sys.argv[i + 3]),float(sys.argv[i + 4]),float(sys.argv[i + 5]))
fx = xy_centroid_rectangle(float(sys.argv[i + 2]),float(sys.argv[i + 4]))
fy = xy_centroid_rectangle(float(sys.argv[i + 3]),float(sys.argv[i + 5]))
i += 6
#if sys.argv[i + 1] == "circ":
#f = area_circle(float(sys.argv[i + 2]),float(sys.argv[i + 3]),float(sys.argv[i + 4]))
#fx = xy_centroid_circle(foat(sys.argv[i + 2]),float(sys.argv[i + 4]))
#fy = xy_centroid_circle(foat(sys.argv[i + 3]),float(sys.argv[i + 4]))
#i += 5
#if sys.argv[i + 1] == "halfcirc":
#f = area_circle(float(sys.argv[i + 2]),float(sys.argv[i + 3]),float(sys.argv[i + 4]))
#fx = xy_centroid_circle(foat(sys.argv[i + 2]),float(sys.argv[i + 4]))
#fy = xy_centroid_circle(foat(sys.argv[i + 3]),float(sys.argv[i + 4]))
#i += 5
#if sys.argv[i + 1] == "righttri":
#f = area_rectangle(float(sys.argv[i + 2]),float(sys.argv[i + 3]),float(sys.argv[i + 4]),float(sys.argv[i + 5]))
#fx = xy_centroid_rectangle(float(sys.argv[i + 2]),float(sys.argv[i + 4]))
#fy = xy_centroid_rectangle(float(sys.argv[i + 3]),float(sys.argv[i + 5]))
#i += 6
if vz == "+":
x = (x * a + fx * f) / (a + f)
y = (y * a + fy * f) / (a + f)
a = a + f
if vz == "-":
x = (x * a - fx * f) / (a - f)
y = (y * a - fy * f) / (a - f)
a = a - f
print x
print y
print a
Why am I getting this error?
That's not how you run a python program. Open a CMD (Windows) prompt and write your command line there. You'll probably need to add python in front too.
Related
I am want to replace a specific part of the string while using textwrap. But I am getting an unwanted line break. I have tried a lot of ways but still, it is not working. here is my code.
import textwrap
import math
def wrap(string, max_width):
wrapper = textwrap.TextWrapper(width = max_width)
word_list = wrapper.fill(text = string)
return word_list
height, width = map(int,input().split())
square = height * width
i = 0
x = []
while i < square:
x.append("-")
i += 1
k = 0
while k < math.floor(height / 2):
line = math.floor(width / 2) + width * k
x[line] = '|'
x[line + 1]= "."
x[line - 1] = "."
for h in range(1, k + 1):
f = h * 3
line_plus = line + f
line_minus = line - f
x[line_plus] = '|'
x[line_minus] = '|'
x[line_minus - 1] = '.'
x[line_plus - 1] = '.'
x[line_minus + 1] = '.'
q = line_plus + 1
x[q] = '.'
k += 1
a = 0
while a < math.floor(height / 2):
line = math.floor(width / 2) + width * a
line_end = (math.floor(width / 2) + width * a) * (-1)
x[line_end - 1] = '|'
if line > width:
x[line_end + 2] = '|'
x[line_end - 4] = '|'
a += 1
listToStr = ''.join([str(elem) for elem in x])
welcome_pos = math.floor(height / 2) * width + (math.floor(width / 2) - math.floor(7 / 2))
s = listToStr[ 0: welcome_pos] + "welcome" + listToStr[welcome_pos + 7:]
print(wrap(listToStr, width) + "\n")
print(wrap(s, width))
my input is 7 21
Output:
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.
----------welcome----
----------|--|--|----
----------|--|--|----
-------------|-------
---
this is my output. But it is giving a space at line 3 which I don't want. I don't know why this is happening. Please help me.
Maybe this is what you want?
code:
#(above the same)
print(wrap(listToStr,width*height))
print(wrap(s,width*height))
result:
5 4
-..|..|..||--|--||--
-..|..|welcome--||--
I have code that looks like this:
n = 2
disc_weights = np.random.uniform(0, 2 * np.pi, 4*n)
phi = (disc_weights[0] * QubitOperator('X0') +
disc_weights[1] * QubitOperator('Y0') +
disc_weights[2] * QubitOperator('Z0') +
disc_weights[3] * QubitOperator('X1') +
disc_weights[4] * QubitOperator('Y1') +
disc_weights[5] * QubitOperator('Z1') +
disc_weights[6] * QubitOperator('') +
disc_weights[7] * QubitOperator('') )
where QubitOperator is a command in a package I am using. How can I automate this to iterate over X, Y, Z, , 1, 2...n and create phi?
This somewhat does the trick but not quite there yet
phi= functools.reduce(operator.add, (1 * QubitOperator(f'{a}{n}') for a,n in itertools.product(["X", "Y", "Z"], range(n))))
n = 2
disc_weights = np.random.uniform(0, 2 * np.pi, 4*n)
iter_ = itertools.product('XYZ', range(n))
tuple_wrapper = lambda t: f'{t[0]}{t[1]}'
sum_ = 0.0 * QubitOperator('')
for dw in disc_weights:
try:
sum_ += dw * QubitOperator(tuple_wrapper(next(iter_)))
except StopIteration:
sum_ += dw * QubitOperator('')
I want to print this pattern in Python 3 (I'm a beginner):
What i have tried :
n = 5
for x in range(1, (n+5) //2 + 1):
for y in range( (n+5) //2 - x):
print(" ", end = "")
for z in range( (x*2)-1 ):
print("*", end = "")
print()
for x in range( (n+5)// 2 + 1, n + 5):
for y in range(x - (n+5) //2):
print(" ", end = "")
for z in range( (n+5 - x) *2 - 1):
print("*", end = "")
print()
But the result is like this:
How can I make the middle hollow like in the image?
Thanks.
The coordinates of the points on the hollow rhombus satisfies |x|+|y|==m (where m = n-1). Therefore, you could use
In [29]: m = n-1; print('\n'.join([''.join(['*' if abs(row)+abs(col)==m else ' ' for col in range(-m,m+1)]) for row in range(-m,m+1)]))
*
* *
* *
* *
* *
* *
* *
* *
*
or, equivalently, but without list comprehensions:
n = 5
m = n-1
for row in range(-m, m+1):
for col in range(-m, m+1):
if abs(row) + abs(col) == m:
c = '*'
else:
c = ' '
print(c, end='')
print()
To make the solid rhombus, simply change the condition to abs(row) + abs(col) <= m.
With recursion:
def print_stars(i,n):
if i:
print( ' '*(n-i-1) + '*' + ' '*(2*i-1) + '*')
else:
print( ' '*(n-1) + '*')
# recursive
def r(n, i=0):
print_stars(i,n) # up
if i<n-1:
r(n, i+1) # recurse
print_stars(i,n) # down
# start
r(5)
prints:
*
* *
* *
* *
* *
* *
* *
* *
*
Simple python code without using functions
rows = 7
for i in range(1, rows + 1):
for j in range(1, rows - i + 1):
print(end = ' ')
for k in range(1, 2 * i):
if k == 1 or k == i * 2 - 1:
print('*', end = '')
else:
print(' ', end = '')
print()
for i in range(rows - 1, 0, -1):
for j in range(1, rows - i + 1):
print(' ', end = '')
for k in range(1, 2 * i):
if k == 1 or k == i * 2 - 1:
print('*', end = '')
else:
print(' ', end = '')
print()
Output:
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
How to make a "Hollow Diamond In Python 3.10"
You can use this code here:
my personal code
Currently prints range values to terminal. I need to export these values to a csv file. "print S[t-1], I[t-1], R[t-1]" are the values...
import matplotlib.pyplot as plt
import numpy
beta = 0.24
gamma = 0.142857
Tstart = 0
Tend = 151
r = 0
s = (306.8 * 10**6)
i = (22 * 10**6)
def compute_next_day(t,R,I,S):
R[t] = gamma * I[t - 1] + R[t - 1]
I[t] = (beta * I[t-1] * S[t-1] / (r+i+s)) - gamma * I[t-1] + I[t-1]
S[t] = - (beta * I[t-1] * S[t-1] / (r+i+s)) + S[t-1]
print S[t-1], I[t-1], R[t-1]
def compute_entire_period(Tstart, Tend, R, I, S):
R[Tstart] = r
I[Tstart] = i
S[Tstart] = s
for t in range(Tstart + 1, Tend):
compute_next_day(t, R, I, S)
R = range(Tstart, Tend)
I = range(Tstart, Tend)
S = range(Tstart, Tend)
If you want to save data in a CSV, use the dsv library:
import csv
beta = 0.24
gamma = 0.142857
Tstart = 0
Tend = 151
r = 0
s = (306.8 * 10 ** 6)
i = (22 * 10 ** 6)
def compute_next_day(t, R, I, S):
R[t] = gamma * I[t - 1] + R[t - 1]
I[t] = (beta * I[t - 1] * S[t - 1] / (r + i + s)) - gamma * I[t - 1] + I[t - 1]
S[t] = - (beta * I[t - 1] * S[t - 1] / (r + i + s)) + S[t - 1]
return [S[t-1], I[t-1], R[t-1]]
def compute_entire_period(Tstart, Tend, R, I, S):
R[Tstart] = r
I[Tstart] = i
S[Tstart] = s
with open('mydata.csv', 'w') as mycsvfile:
writer = csv.writer(mycsvfile)
for t in range(Tstart + 1, Tend):
writer.writerow(compute_next_day(t, R, I, S))
R = range(Tstart, Tend)
I = range(Tstart, Tend)
S = range(Tstart, Tend)
compute_entire_period(Tstart, Tend, R, I, S)
Something like this should work:
def compute_next_day(t,R,I,S,):
...
return S[t-1], I[t-1], R[t-1] # return values instead of printing to terminal
with open('path_to_file.csv','w') as f:
for ... : # your choice of loop
vals = compute_next_day(...) # compute your values
f.write(','.join(vals) + '\n') # dump your values separated with ','
I've been working with this equations of Potential Flow
pSI: 0 = 10 * y + 23.8732414638 * (pi + atan(abs((x - 12) / x))) + 23.8732414638 * (pi - atan(abs((y + 12) / x)))-234.882642242
V: 0 = ((10 + 23.8732414638 * x / (x*2 + (y - 12)*2) + 23.8732414638 * x / (x*2 + (y + 12)*2))**2 + (23.8732414638 * (y - 12) / (x*2 + (y - 12)*2) + 23.8732414638 * (y + 12) / (x*2 + (y + 12)*2))*2)*0.5-8
using the next code to solve them:
# Prototype of N-R for a system of two non-linear equations
# evaluating functions of two variables
from math import *
eq1 = raw_input('Enter the equation 1: ')
eq2 = raw_input('Enter the equation 2: ')
x0 = float(input('Enter x: '))
y0 = float(input('Enter y: '))
def f(x,y):
return eval(eq1)
def g(x,y):
return eval(eq2)
x = x0
y = y0
for n in range(1, 8):
a = (f(x + 1e-06, y) - f(x,y)) / 1e-06
b = (f(x, y + 1e-06) - f(x,y)) / 1e-06
c = - f(x,y)
d = (g(x + 1e-06, y) - g(x,y)) / 1e-06
eE = (g(x, y + 1e-06) - g(x,y)) / 1e-06
fF = - g(x,y)
print "f(x, y)= ", eq1
print "g(x, y)= ", eq2
print """x y """
print x, y
print """a b c d e f """
print a, b, c, d, e, fF
print """
a * x + b * y = c
d * x + e * y = f
"""
print a," * x + ",b," * y = ",c
print d," * x + ",eE," * y = ",fF
_Sy = (c - a * fF / d) / (b - a * eE / d)
_Sx = (fF / d) - (eE / d) * _Sy
Ea_X = (_Sx ** 2 + _Sy ** 2)**0.5
x = x + _Sx
y = y + _Sy
print "Sx = ", _Sx
print "Sy = ", _Sy
print "x = ", x
print "y = ", y
print "|X_1 - X_0| = ", Ea_X
And gives me the Zero division Error
Traceback (most recent call last):
File "C:\Documents and Settings\Principal\Mis documentos\Finished_Non_lin_N_R.py", line 38, in <module>
d = (g(x + 1e-06, y) - g(x,y)) / 1e-06
File "C:\Documents and Settings\Principal\Mis documentos\Finished_Non_lin_N_R.py", line 27, in g
return eval(eq2)
File "<string>", line 1, in <module>
ZeroDivisionError: float division by zero
Then I tried with this one that I've found in one solved question:
from scipy.optimize import fsolve
from math import *
def equations(p):
x, y = p
return ( ((10 + 23.8732414638 * x / (x**2 + (y - 12)**2) + 23.8732414638 * x / (x**2 + (y + 12)**2))**2 + (23.8732414638 * (y -12) / (x**2 + (y - 12)**2) + 23.8732414638 * (y + 12) / (x**2 + (y + 12)**2))**2)**0.5-8, 10 * y + 23.8732414638 * (pi + atan(abs((x - 12) / x))) + 23.8732414638 * (pi - atan(abs((y + 12) / x)))-234.882642242)
x, y = fsolve(equations, (0, 18))
print equations((x, y))
And with the next problem:
<module2>:19: RuntimeWarning: divide by zero encountered in long_scalars
<module2>:19: RuntimeWarning: divide by zero encountered in double_scalars
(-1.3374190643844486e-11, -2.8308022592682391e-11)
And the last part of this story is that I have some code that actually works
but is too clumsy and a little bit inaccurate, and I would like some suggestions in order to make it simple:
from math import *
U = 10
b = 15
h = 12
cF = 0.5 * U * b / pi
pSI0 = 234.882642242
VP = 12.0
X0 = 0
Y0 = 40
sX = 0.01
sY = 0.01
print cF
def FirstFor(A,B,C,D):
for n in range(1,808):
for m in range(1,4002):
X = A - B*n
Y = C - D*m
pSI = U * Y + cF * (pi + atan(abs((Y - h) / X))) + cF * (pi - atan(abs((Y + h) / X)))
Vaprox = ((10 + 23.8732414638 * X / (X**2 + (Y - 12)**2) + 23.8732414638 * X / (X**2 + (Y + 12)**2))**2 +(23.8732414638 * (Y - 12) / (X**2 + (Y - 12)**2) + 23.8732414638 * (Y + 12) / (X**2 + (Y + 12)**2))**2)**0.5
EA1 = abs(pSI0 - pSI)
EA2 = abs(VP - Vaprox)
if EA1 < 0.05 and EA2 < 0.05:
X1f = X
Y1f = Y
H3 = [X1f,Y1f]
print n,m,X,Y,"GG son",EA1,EA2
print H3
for n in range(1,808):
for m in range(1,4002):
X = H3[0] - B*n*0.001
Y = H3[1] - D*m*0.001
pSI = U * Y + cF * (pi + atan(abs((Y - h) / X))) + cF * (pi - atan(abs((Y + h) / X)))
Vaprox = ((10 + 23.8732414638 * X / (X**2 + (Y - 12)**2) + 23.8732414638 * X / (X**2 + (Y + 12)**2))**2 +(23.8732414638 * (Y - 12) / (X**2 + (Y - 12)**2) + 23.8732414638 * (Y + 12) / (X**2 + (Y + 12)**2))**2)**0.5
EA1 = abs(pSI0 - pSI)
EA2 = abs(VP - Vaprox)
if EA1 < 0.0001 and EA2 < 0.0001:
X2f = X
Y2f = Y
I3 = [X2f,Y2f]
print n,m,X,Y,"GG son",EA1,EA2
print I3
for n in range(1,808):
for m in range(1,4002):
X = H3[0] + B*n*0.001
Y = H3[1] + D*m*0.001
pSI = U * Y + cF * (pi + atan(abs((Y - h) / X))) + cF * (pi - atan(abs((Y + h) / X)))
Vaprox = ((10 + 23.8732414638 * X / (X**2 + (Y - 12)**2) + 23.8732414638 * X / (X**2 + (Y + 12)**2))**2 +(23.8732414638 * (Y - 12) / (X**2 + (Y - 12)**2) + 23.8732414638 * (Y + 12) / (X**2 + (Y + 12)**2))**2)**0.5
EA1 = abs(pSI0 - pSI)
EA2 = abs(VP - Vaprox)
if EA1 < 0.0001 and EA2 < 0.0001:
X2f = X
Y2f = Y
I3 = [X2f,Y2f]
print n,m,X,Y,"GG son",EA1,EA2
print I3
# starting with the FirstFor
FirstFor(X0,sX,Y0,sY)
print "\n This should be good"
raw_input()