关于#python#的问题,如何解决?

img

img

img

要求在截图里,我也翻译成了中文,有没有可以帮忙做一下
要求在截图里,我也翻译成了中文,有没有可以帮忙做一下
要求在截图里,我也翻译成了中文,有没有可以帮忙做一下
要求在截图里,我也翻译成了中文,有没有可以帮忙做一下

def displayCounties(stateName):
    data = []
    with open('teenpreganc.txt') as f:
        readlines = f.readlines()
        readlines = readlines[1:]
        for i in readlines:
            temp = i.rstrip()
            data.append(temp.split(','))
    result = []
    for i in data:
        # 判断stateName和输入的是否一致,同时结果集中该County不存在
        if i[1] == stateName and i[2] not in result:
            result.append(i[2])
    if len(result) == 0:
        print('Invalid state was entered')
        exit(1)
    else:
        for i in result:
            print(i)


while True:
    print("please input the state name:")
    displayCounties(input())
import pandas as pd
import prettytable as pt 

df = pd.read_csv('teenpregnancy.txt',delimiter = ',')
def displayCountries(df1, state):
    return df1.loc[df1['State']==state]

state = input(">>>")
res = displayCountries(df, state)
ppt = pt.PrettyTable(['CountyName'])
r = set(res['County'].tolist())
if r:    
    rows = [[i] for i in r]
    ppt.add_rows(rows)
    print(f"State Name:{state}")
    print(ppt)
else:
    print("Invalid state was entered")
    
--result
>>>Alabama
State Name:Alabama
+------------+
| CountyName |
+------------+
|  Baldwin   |
|  Barbour   |
|  Autauga   |
+------------+


def displayCounties(stateToDisplay):
    data = {}  #州名字典
    with open('teenpreganc.txt') as f:
        for line in f:  # 按行读取,不用管是否是第一行
            s=line.split(',')  #按逗号分解
            state=s[1]
            county=s[2]
            if state not in data:  #如果州名不在字典里,则加入
                data[state]=[county]
            elif county not in data[state]:  #如果县名不在list里,则加入
                data[state].append(county)
    if stateToDisplay in data:
        print(f'The county in {stateToDisplay} are')
        for x in data[stateToDisplay]:  #循环打印,每个一行
            print(x)
    else:
        print('Invalid state was entered')
    
 

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632