【问题标题】:Can I get Last Digit of an Integer in Python我可以在 Python 中获取整数的最后一位吗
【发布时间】:2020-07-23 01:30:36
【问题描述】:

我正在尝试查找整数的最后一位。但无法找到它。

Note -我正在工作盘中使用 .split()。

# cooking my dish here

x = str(input())

letters = ""
words = x.split()

for word in words:
    letters += word[0]
    a = int(word(0) + word[-1])
    print(a)

我想用这个错误来结束所有的事情。

21
Traceback (most recent call last):
  File "E:\testererterset.py", line 9, in <module>
    a = int(word(0) + word[-1])
TypeError: 'str' object is not callable
>>> 

我需要认真的帮助。如果您希望我非常了解,您也可以在下面分享您的代码。 到那时为止!

【问题讨论】:

  • word(0) -> word[0]
  • 您使用圆括号来获取第一个字符:a = int(word(0) + word[-1]) 这是错误的,您需要将其写为:a = int(word [0] + 单词[-1])

标签: python function split integer typeerror


【解决方案1】:
word(0)

TypeError: 'str' 对象不可调用

word[0]

应该可以。

【讨论】:

  • 彼得,感谢您的回复。也感谢您的快速。但是当我运行相同的代码时。通过在输入中写入一个数字,它不会以列出的形式返回我的数字,尽管我在上面的代码中提到我作为字符串输入并作为整数返回。但该代码只是返回我在输入中添加的相同数字。我可以寻求帮助吗?来自你?
  • 考虑这个How to create a Minimal, Reproducible Example 并特别提供您的输入和预期输出。
  • ... 并考虑基本的 Stackoverflow 礼仪规则:What should I do when someone answers my question?
猜你喜欢
  • 1970-01-01
  • 2011-06-01
  • 2011-03-09
  • 1970-01-01
  • 2013-06-13
  • 2017-11-17
  • 2014-07-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多