[英]How do I get the city output to append itself into initials and then print it?
**確保在任務 1 中創建並運行代碼以導入城市.txt
創建並運行代碼以將城市.txt 重新打開為城市文件
read() city_file 到一個名為城市的變量中
遍歷城市中的字符 a. 測試是否 .isupper(),如果 True 將字符附加到字符串變量:首字母 c。 Else if (elif) 字符是 "\\n",如果 True 將 "\\n" 附加到首字母**
打印首字母
city_file = open('cities.txt', 'r')
城市 = city_file.read()
首字母 = " "
對於城市中的城市:
如果 city.isupper() == True:
city += initials
elif 城市 == "\\n":
city += initials
打印(首字母)
你必須去掉“== 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.