Python: 从起点到定义范围打印列表
Python: Printing a List from a starting point to a defined range
这是我的代码;我知道它一团糟,但如果那样的话,我只是在学习基础知识。我的问题是最后打印一个列表。你看,我已经尝试了很多东西,但它一直告诉我“'list' 对象不可调用”或 "Unexpected type: Tuple..." 现在,元组对我来说没有任何意义,因为我们还没有在 class 中没有达到那个点,但我还是不想要一个元组。我想打印一个列表,该列表从指定点 "f" 开始,范围为 "f2"。但我就是无法理解。
我做错了什么?
def main():
f = int(input("Starting Place in Code: "))
message = input("simple message: ")
f2 = len(message)
messageList = []
fibonacciNumbers = [0, 1]
messageNumList = []
f3 = fibonacciNumbers[f: f+f2]
for let in message:
messageList.append(let)
for let in messageList:
messageNumList.append(ord(let))
for i in range(2, 700):
fibonacciNumbers.append(fibonacciNumbers[i - 1] + fibonacciNumbers[i - 2])
#print(fibonacciNumbers)
print(f3)
print(f2)
print(messageList)
print(messageNumList)
main()
Starting Place in Code: 5
simple message: fish
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...]
[5, 8, 13, 21, 34, 55]
4
['f', 'i', 's', 'h']
[102, 105, 115, 104]
Process finished with exit code 0
我认为您已经确定问题出在 fibonacciNumbers[f, f2]
。所以分为两部分:
1) 切片列表的正确语法是什么?用 :
分隔索引,而不是 ,
。所以 fibonacciNumbers[f:f2]
(请注意,切片中的最后一个索引将是 f2-1
,因此您可能需要 fibonacciNumbers[f:f2+1]
)。 (编辑:要清楚 l[i:j]
意味着 "give me elements i, i+1, ..., j-1
of list l
, not " 给我 j
个从索引 i
开始的元素。”)
2) 错误是什么意思?元组就像一个列表,但是是固定的——您不能更改或添加元素:(1,2,3)
是一个元组,[1,2,3]
是一个列表。有时括号是不必要的。当您写 fibonacciNumbers[f, f2]
时,Python 将其解释为 "give me the element of the list whose index is the tuple (f,f2)
",这是没有意义的,因此是例外。 (例如,您可以通过尝试访问列表的元素 "a"
来查看变化。)
我猜“'list' object is not callable”发生在你写类似 fibonacciNumbers(f,f2)
的时候。使用括号,您正在调用一个名为 fibonacciNumbers
的函数,但它是一个列表而不是函数,因此不可调用。
这是我的代码;我知道它一团糟,但如果那样的话,我只是在学习基础知识。我的问题是最后打印一个列表。你看,我已经尝试了很多东西,但它一直告诉我“'list' 对象不可调用”或 "Unexpected type: Tuple..." 现在,元组对我来说没有任何意义,因为我们还没有在 class 中没有达到那个点,但我还是不想要一个元组。我想打印一个列表,该列表从指定点 "f" 开始,范围为 "f2"。但我就是无法理解。 我做错了什么?
def main():
f = int(input("Starting Place in Code: "))
message = input("simple message: ")
f2 = len(message)
messageList = []
fibonacciNumbers = [0, 1]
messageNumList = []
f3 = fibonacciNumbers[f: f+f2]
for let in message:
messageList.append(let)
for let in messageList:
messageNumList.append(ord(let))
for i in range(2, 700):
fibonacciNumbers.append(fibonacciNumbers[i - 1] + fibonacciNumbers[i - 2])
#print(fibonacciNumbers)
print(f3)
print(f2)
print(messageList)
print(messageNumList)
main()
Starting Place in Code: 5
simple message: fish
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...]
[5, 8, 13, 21, 34, 55]
4
['f', 'i', 's', 'h']
[102, 105, 115, 104]
Process finished with exit code 0
我认为您已经确定问题出在 fibonacciNumbers[f, f2]
。所以分为两部分:
1) 切片列表的正确语法是什么?用 :
分隔索引,而不是 ,
。所以 fibonacciNumbers[f:f2]
(请注意,切片中的最后一个索引将是 f2-1
,因此您可能需要 fibonacciNumbers[f:f2+1]
)。 (编辑:要清楚 l[i:j]
意味着 "give me elements i, i+1, ..., j-1
of list l
, not " 给我 j
个从索引 i
开始的元素。”)
2) 错误是什么意思?元组就像一个列表,但是是固定的——您不能更改或添加元素:(1,2,3)
是一个元组,[1,2,3]
是一个列表。有时括号是不必要的。当您写 fibonacciNumbers[f, f2]
时,Python 将其解释为 "give me the element of the list whose index is the tuple (f,f2)
",这是没有意义的,因此是例外。 (例如,您可以通过尝试访问列表的元素 "a"
来查看变化。)
我猜“'list' object is not callable”发生在你写类似 fibonacciNumbers(f,f2)
的时候。使用括号,您正在调用一个名为 fibonacciNumbers
的函数,但它是一个列表而不是函数,因此不可调用。