【问题标题】:Python: count length of word [duplicate]Python:计算单词的长度[重复]
【发布时间】:2017-10-09 21:09:26
【问题描述】:

我目前有这个任务:

word = input("Please enter a word: ")

print("The length of " + word + " is " + len(word = int(word)))

它运行,我输入Lilith Qua

我遇到一个错误说:

ValueError: invalid literal for int() with base 10: 'Lilith Qua'

有办法解决这个问题吗?

【问题讨论】:

  • 您希望len(word = int(word)) 做什么? , len(word) 就足够了。
  • 顺便说一句,最好使用string formatting 而不是串联。 print("The length of {} is {}".format(word, len(word))) 它还负责转换。

标签: python


【解决方案1】:

在连接之前,您必须将 int 转换为字符串。

你必须使用

print("The length of " + word + " is " + str(len(word)))

字符串格式也可以用作,

print("The length of %s is %d"%(word,len(word)))

这里,

  • %s 用于字符串

  • %d 用于 int

【讨论】:

  • print("" + word + "的长度是" + str(len(word))) 是正确的答案 谢谢。你帮帮我吧。
猜你喜欢
  • 2020-07-12
  • 1970-01-01
  • 2016-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 1970-01-01
  • 2013-06-15
相关资源
最近更新 更多