【问题标题】:String counts python with a function and input使用函数和输入对 python 进行字符串计数
【发布时间】:2021-08-13 00:50:38
【问题描述】:
我有这个任务:
创建一个程序,计算并在终端中显示以下单词的字符数。程序必须至少包含一个函数。
这是我创建的:
value = input("Write your word here:")
def word(value):
word = 0
for i in len(int(value)):
print(value)
我如何解决这个任务?
【问题讨论】:
标签:
python
string
function
variables
input
【解决方案1】:
您的代码必须进行编辑。由于您是初学者,我为您添加了一些解释。
作为初学者,您可以查看一些 Python 教程来学习 Python。
# write your function, which will iterate over the value
# and print each character
def word(value):
for i in range(len(value)): # range, instead of len(value), int values are not iterable
print(value[i]) # print the i-th value
# you can directly iterate on value which is a string
for c in value:
print(c)
value = input("Write your word here:")
word(value)