【问题标题】:user(0) Traceback (most recent call last):user(0) Traceback(最近一次调用最后一次):
【发布时间】:2015-10-08 07:19:13
【问题描述】:

我叫米奇。我以前在堆栈交换的数学和物理方面得到了一些惊人的帮助。我认为溢出是相关的,但我意识到我应该将此保留为一个问题。我已经尝试过多次尝试向自己介绍编程,尤其是 python,阅读整本书大小的 PDF 和扩展的 YouTube 视频,并不断尝试必须真正非常基本的东西。

所以我再次尝试,希望堆栈社区可以帮助我完成这些小旅行。我不知道你们为什么要花这么多精力帮助像我这样的菜鸟免费学习东西,但我很感激你们这样做。希望有一天我能精通某件事来回馈社会。所以这是我的第一个问题。

我已经下载了 python 3.4.1。那里的很多教程似乎都在教 python 2.x,但我很确定 3。我在 Windows 计算机上。我在 thenewboston 的 YouTube 上找到了一个视频系列。我和他一样使用 IDLE。在他的版本中,我遇到了麻烦:

>>> user = "Tuna McFish"
>>> user(0)

'T'

当我这样做时:

>>> user = "Tuna McFish"
>>> user(0)
Traceback (most recent call last): 
File "<pyshell#1>", line 1, in <module>   
user(0)
TypeError: 'str' object is not callable

(抱歉,不知道如何正确设置此网站的格式。我做错了什么?)

【问题讨论】:

  • try user[0] user here is not a function by the way your first method should also throw error
  • [0] 有效。谢谢!
  • 你确实知道你做错了什么,你试图把它当作一个函数来调用
  • 仍然处于非常基本的水平,所以是和否,但之前是 () 现在是 []。我知道 print 是一个函数,而 print 你使用()。长度也是 len。就像我说的那样仍然非常基本,我意识到我听起来像痉挛。
  • method [] 主要用于slicing and indexing it is not a function call.It is also used to create list `() 用于调用函数创建函数 print 是3.x 中的函数也用于创建元组、生成器等' Happy学习

标签: python youtube typeerror traceback


【解决方案1】:

欢迎来到 Stack Overflow 和一般编程。请保持希望,并被告知在编程中您需要非常精确,所有不同的括号都有不同的含义。在python中,您至少具有以下基本含义。

方括号 [] 用于列表和切片

见http://www.dotnetperls.com/list-python

>>> words = ['Some', 'different', 'words']
>>> words[1]
'different' 

见http://www.dotnetperls.com/slice

>>> words[:2]          # Slice of two first elements
['Some', 'different']

注意!请注意,方括号还用于取消引用各种内容,如字典、元组、列表等...

花括号 {} 表示字典或集合

见http://www.dotnetperls.com/dictionary-python

>>> lookup = { 1 : 'First', 2: 'Second', 3: 'Third' }
>>> lookup[1]       # NB! Using square brackets for looking up
'First'

http://www.dotnetperls.com/set-python

>>> colors = { 'red', 'green', 'blue'}
>>> 'red' in colors
True

普通括号 () 用于元组、函数、分组......

见http://www.dotnetperls.com/tuple-python

>>> block = ( '10th East Street', '30th North Avenue') 
>>> horizontal, vertical = block  
>>> horizontal
'10th East Street'
>>> vertical
'30th North Avenue'
>>> block[0]       # NB! Using square brackets for dereferencing
'10th East Street'

在函数中,见http://www.dotnetperls.com/def

>>> print('a function call')
a function call

在分组中,请参阅“和/或”部分中的http://www.dotnetperls.com/if-python

>>> a, b, c = True, False, True
>>> a and (b or c)
True

【讨论】:

    猜你喜欢
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多