【问题标题】:How do I get the city output to append itself into initials and then print it?如何让城市输出将自己附加到首字母然后打印出来?
【发布时间】:2021-02-27 04:27:25
【问题描述】:

**确保代码已创建并在任务 1 中运行以导入 city.txt

  1. 创建并运行代码以重新打开 citys.txt 作为 city_file

  2. read() city_file 到一个名为ities的变量中

  3. 遍历城市中的字符 a.测试 if .isupper(),如果 True 将字符附加到字符串变量:initials c。 Else if (elif) 字符为“\n”,如果为 True,则将“\n”附加到首字母**

  4. 打印姓名缩写

cities_file = open('cities.txt', 'r')

城市 = 城市文件.read()

首字母 = " "

对于城市中的城市:

如果 city.isupper() == 真:

city += initials

elif city == "\n":

city += initials

打印(首字母)

【问题讨论】:

    标签: python if-statement append


    【解决方案1】:

    你必须去掉“== True”部分——这是唯一的问题

    initials = ""
    cities_file = open("cities.txt", "r")
    cities = cities_file.read()
    for c in cities:
        if c.isupper():
    initials += c
        elif c == "\n":
    initials += c
    print(initials)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      • 2022-12-10
      相关资源
      最近更新 更多