python - 创建一个程序,用笛卡尔坐标和极坐标计算矢量结果
问题描述
我需要创建一个计算矢量结果的程序,但我需要允许用户在极坐标和笛卡尔坐标之间进行选择。这是我到目前为止所拥有的,但它给了我错误,我不确定如何询问用户 x,y 坐标以及如何在输入向量后让程序停止。请帮忙,我对这个了解不多!
导入数学 #calculate 向量分量并从极坐标变为笛卡尔坐标,反之亦然
def fxcomponent(x, y): # to find cartesian x
number = x
y = math.cos(math.degrees(y))
result = number * y
return result
def fycomponent(x, angle): #to find cartesian y
number = x
angle = math.sin(math.degrees(angle))
result = number * angle
return result
def polar_length(r,t): #to find polar lenght
number = r
length =((r**2) + (t**2))
result = math.sqrt(number)
return result
def polar_angle(r,theta): #to find polar angle
result = math.atan2(theta,r)
return result
#prompts
prompt = 'Hi!'
type_coor = input('Enter the type of coordinate you will use: use p for polar \n enter c for cartesian')
vector_number = input('Enter the number of vectors you will be adding: ')
i = 0
while i <= vector_number:
if type_coor == 'p': #if polar values are entered
print('What is the r value?:')
x, angle = [int(x) for x in input('>').split()]
result = fxcomponent(x, angle)
print('The x component of %d at %d° is: %d' % (x, angle, result))
elif type_coor == 'p':
print ('What is the value of the angle?:')
y, angle = [int(x) for x in input('>').split()]
result = fycomponent(y, angle)
print('The y component of %d at %d° is: %d' % (y, angle, result))```
解决方案
这里有很多不同的错误来源,这是一项艰巨的任务......
首先,如果有人输入了意想不到的东西,这段代码就会中断,
要解决此问题,您可以使用 if 条件来检查输入值是否正常,如下所示:
variable = input('enter "c" or "p" \n>>')
if variable not in ['c','p']: # to check if variable is in the list
exit() # quit the program
# and for a number (integer only !!!)
variable = input('enter a number \n>>')
if !variable.isnumeric():
exit()
那么,我对您在这里使用的数学了解不多,但是,如果您在顶部的 4 个函数是正确的,那么您应该要求每个变量具有不同的输入,并使用您的if ... elif
only 来区分type_coord == 'p'
和type_coord == 'c'
像这样的东西有效,但它只适用于整数,我相信这不是你想要的:
#prompts
prompt = 'Hi!'
type_coor = input('Enter the type of coordinate you will use: \nuse p for polar \nenter c for cartesian \n("q" for quit):\n>> ')
if type_coor not in ['c', 'p']:
print('coordinate type must be "c" or "p"')
exit() # quitting the program
vector_number = input('Enter the number of vectors you will be adding: \n>> ')
if vector_number.isnumeric() == False:
print('must be integer')
exit()
vector_number = int(vector_number)
i = 0
while i < vector_number:
if type_coor == 'p': #if polar values are entered
r = input('What is the r value?: \n>>')
if r.isnumeric() == False:
print('r must be integer')
exit()
r = int(r)
t = input('What is the t value?: \n>>')
if t.isnumeric() == False:
print('theta must be integer')
exit()
t = int(t)
theta = input('What is the theta value?: \n>>')
if theta.isnumeric() == False:
print('theta must be integer')
exit()
theta = int(theta)
length = polar_length(r, t)
angle = polar_angle(r,theta)
print('length = %d' % (length))
print('angle = %d' % (angle))
elif type_coor == 'c': # if cartesian values are entered
# do the same for cartesian coordinates
i += 1
如果您想使用十进制值,那么代码会变得更加复杂。对于每个浮点值,您应该执行以下操作:
variable = input('enter a decimal \n>> ')
try:
variable = float(variable)
except ValueError:
print('not a decimal')
exit()
...最后一步是用循环替换所有if
输入条件检查,这些while
循环要求用户输入一个值,直到该值正确为止...
希望这会有所帮助,玩得开心!
推荐阅读
- python - 在 pytz 中查找时区名称是否已弃用
- c# - 如何从另一个窗口调用 MainWindow 方法?
- python - 将 Python 变量写入多个文本文件
- c# - 在自己的构造函数中分配对象值
- java - 仅仅为了分层结构而拥有抽象类是否有意义?
- python - Keras LSTM - 时间序列预测的输入形状
- angular - 如何在 Angular 中推送到表单数组([])
- php - 使用 htaccess 将 http://www.example.com/ 重定向到 https://www.example.com/
- php - 如何使用自定义比较函数计算对象数组的交集?
- java - 具有多对多组合的 Hibernate 复合模式