简体   繁体   English

如何更改功能或避免错误

[英]how can I change the function or avoid the error

I don't know if I wrote wrong the "line" function or if is something else in the final statement after "for", help me please.我不知道我是否写错了“line”函数,或者在“for”之后的最后一条语句中是否还有其他内容,请帮助我。 The program is about slope and compare between this values but first I need to find them, but something is not work.该程序是关于斜率和这些值之间的比较,但首先我需要找到它们,但有些东西不起作用。 The code is the next:代码如下:

import math

N = int(input("Number of points: "))

def line(x0,y0,x1,y1):
  if(x0==x1):
    print("\nThe slope doesn't exist\n")
    return None
  if((x0-x1)!=0):
    m = (y1-y0)/(x1-x0)
    return m

for i in range(N):
  for j in range(N):
    ind = None
    for ind in range(N):
      x_ind = {}
      y_ind = {}
      x_ind[i] = float(input("Enter x_" + str(ind) + ": "))
      y_ind[j] = float(input("Enter y_" + str(ind) + ": "))
    for _ in range(math.factorial(N-1)):
      line(x_ind[i], y_ind[j], x_ind[i+1], y_ind[j+1])
      

TL;DR - You are declaring your dictionaries inside for loop, so they are getting reset with every new iteration. TL;DR - 您在 for 循环中声明您的字典,因此每次新迭代都会重置它们。


I think you're trying to do this -我认为你正在尝试这样做 -

N = int(input("Number of points: "))

def line(x0,y0,x1,y1):
  # calculate slope for (x0,y0) and (x1,y1)
  if x0 == x1:            # it will be a vertical line, it has 'undefined' slope
    # print("The slope doesn't exist\n")
    return None           # Slope Undefined
  else:                   # this is implied, no need to put extra check --> x0-x1 != 0:
    return (y1-y0)/(x1-x0)
  pass

# declare variables
x_ind = {}
y_ind = {}
for i in range(N):
  # read inputs and update the existing variables
  x_ind[i] = float(input("Enter x_" + str(i) + ": "))
  y_ind[i] = float(input("Enter y_" + str(i) + ": "))
  print(x_ind, '\n', y_ind)

# calculate slope for every pair of points
for j in range(N):
  for k in range(j+1,N):
    m = line(x_ind[j], y_ind[j], x_ind[k], y_ind[k])
    print(f'slope of line made using points: ({x_ind[j]}, {y_ind[j]}) and ({x_ind[k]}, {y_ind[k]}) is {m}')

Sample Input:样本输入:

Number of points: 3

Enter x_0: 3
Enter y_0: 0

Enter x_1: 0
Enter y_1: 4

Enter x_2: 0
Enter y_2: 0

Sample Output:示例输出:

slope of line made using points: (3.0, 0.0) and (0.0, 4.0) is -1.3333333333333333
slope of line made using points: (3.0, 0.0) and (0.0, 0.0) is -0.0
slope of line made using points: (0.0, 4.0) and (0.0, 0.0) is None

Try using lists and not dictionary in order to work with index values correctly:尝试使用列表而不是字典来正确处理索引值:

x_ind = []
y_ind = []

Since the lists are empty, you may use the append() method to push elements to the lists as I can see that is what you intend to do.由于列表是空的,您可以使用append()方法将元素推送到列表,因为我可以看到这是您打算做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我改变我正在迭代的列表时,如何避免代码中的列表错误功能? - How can I avoid the list error function in my code as I alter a list I am iterating? 如何在程序中实现控制功能并避免AttributeError:'module'对象没有属性错误 - How can I implement a control function in my program and avoid the AttributeError: 'module' object has no attribute error 如何避免解释器检查函数的默认参数? - How can I avoid the interpreter checking the function default arguments? 如何使用回调 function 避免全局变量 - How can I avoid global variable with callback function 如何避免导入底图时出现 PROJ_LIB 错误? - How can I avoid PROJ_LIB error in importing basemap? 如何避免出现“当前绘制对象”错误? - How can I avoid “Object currently drawn” error? 如何避免此错误“SyntaxError:解析时意外 EOF” - How can I avoid this error "SyntaxError: unexpected EOF while parsing" WebScraping时如何避免Http协议错误0 - How can I avoid Http protocol error 0 while WebScraping 如何改善代码以避免基于内存的错误? - How can I improve my code to avoid memory based error? 如何在我的 pandas apply 语句中避免此索引错误? - How can I avoid this indices error in my pandas apply statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM