【问题标题】:How do you append values to list then iterate through lists?如何将值附加到列表然后遍历列表?
【发布时间】:2016-08-21 01:09:44
【问题描述】:

我正在尝试为我的 TI-84 编写一个基本程序,用于查找 2d 平面上的多边形面积。作为参考,我用python写过很多次了,这是它的作用:

x_list,y_list,verts,tot_1,tot_2=[],[],int(input("How many vertices are on the polygon: ")),0,0 //sets vars to defaults and gets num of vertices
for i in range(verts): //gets X and Y values of each point for num. of vertices
    x_list.append(float(input("X value of point %s: " % str(i+1)))) //appends x value given to x list
    y_list.append(float(input("Y value of point %s: " % str(i+1))))  //appends y value given to y list
for ind in range(verts-1):
    tot_1 += (x_list[ind]*y_list[ind+1])-(y_list[ind]*x_list[ind+1]) 
print(str(abs((tot_1)/2))) //prints area: abs value of total over two

这只是执行常规数学中也显示的非常基本的算法:http://www.mathopenref.com/coordpolygonarea.html

现在,当我尝试在 TI-Basic 中编写相同的内容(使用 TI Connect 应用程序并发送到计算器)时,它会在第一次引用其中一个列表时返回语法错误; “检查输入的所有参数”。这条线被星号包围。 cmets不在实际代码中

ClrHome //clears screen
Prompt V //gets number of vertices
0→T //sets total to 0
Disp V //displays vertices, was used for testing
For(N,1,V,1) //runs code for number of vertices
Input "x val: ",X //gets latest x val
Input "y val: ",Y //gets latest y val
**X→L1(1+dim(L1))**  //appends x to listand 
Y→L2(1+dim(L2))      //y to list
End //end for

For(I,1,P,1)
T+((L1(I)*L2(I+1))-(L2(I)*L1(I+1))→T //adds up total
End

Disp abs(T/2)

当通过将 L1 更改为 list1 字符并将 L2 更改为 list2 字符来更改计算器上的代码时,它所做的只是返回值 12.5*number of vertices-2。我的问题是:

  1. 如何在计算机上的代码中表示列表?当我在代码中写 L1 时,它实际上不是我认为的内置列表变量,这就是导致语法错误的原因。我认为。
  2. 我需要重置列表变量吗?我第一次测试这个时, 12.5*vertices-2 起作用了,所以它只是将列表变量永久设置为那个,现在当它在程序的以后运行中添加东西到列表时,它永远不会达到那些索引?
  3. 代码是否存在任何缺陷,导致其根本无法工作?我对 TI-Basic 完全陌生。

【问题讨论】:

    标签: list ti-basic


    【解决方案1】:

    知道了,我很傻。

    1st,使用了ti-connect中语法参考中给出的列表变量

    第二,第二个for循环的算法错误。

    【讨论】:

      【解决方案2】:
      1. 使用 2ND 键键入列表变量。

      2. 要重置列表,有两种方法:在程序末尾输入DelVar L1 以删除变量(DelVar 位于 PRGM 菜单中),或者,如果您想保留变量但仍会删除内容,您可以在程序开始时通过使用0→dim(L1) 将其大小设置为零来清除列表(dim( 位于 LIST 菜单中)。让程序自行清理并在运行后删除不必要的变量也是一种很好的做法。

      3. 您似乎已经自己弄清楚了算法。

      欢迎来到 TI-BASIC!

      【讨论】:

        猜你喜欢
        • 2012-10-05
        • 2021-09-27
        • 2019-08-20
        • 1970-01-01
        • 2016-02-26
        • 2019-11-06
        • 1970-01-01
        • 2018-10-28
        • 1970-01-01
        相关资源
        最近更新 更多