【问题标题】:How to write a program in python which prints the fist letter of every word of the input string without using built-in fuctions?如何在 python 中编写一个程序,在不使用内置函数的情况下打印输入字符串的每个单词的第一个字母?
【发布时间】:2020-09-11 23:46:43
【问题描述】:
sen=input ("Enter a sentence: ")
index=0

for i in sen:
   if i<=len(sen):
       if sen[i]!=' ':
           print(sen[i])
exit()

【问题讨论】:

  • for i in sen: 没有给出循环索引,所以if i&lt;=len(sen): 将不起作用,因为您将尝试将字符串与整数进行比较(由len() 给出)
  • 先生,我是 python 新手,所以会出错。请帮助我改进它。

标签: python python-3.x python-3.5


【解决方案1】:

你可以只遍历字符串,检查空格,然后在空格后面取字符:

def main(string):
    count = 0
    output = []
    for _ in string:
        if string[count] == ' ':
            output.append(string[count + 1])
        count += 1
    return output
        

【讨论】:

    猜你喜欢
    • 2021-06-21
    • 2022-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-07
    • 2020-02-01
    相关资源
    最近更新 更多