亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

cs50 pset6 可讀性得到錯誤等級和負索引

cs50 pset6 可讀性得到錯誤等級和負索引

米琪卡哇伊 2022-10-11 21:16:56
誰能發現這個問題?每次我的索引都為負數,所以它總是打印“Before Grade 1”。我嘗試做一些正則表達式,但我仍然不知道如何真正實現它import refrom cs50 import get_stringwords = 1letters = 0sentences = 0st = get_string("Text: ")t = len(st)regex = r'\w+'output = re.findall(regex,st)for i in range(t):    if st.isalpha():        letters += 1    if st.isspace():        words += 1    if st[i] == '.' or st[i] == '!' or st[i] == '?':        sentences += 1L = (letters / words * 100)S = (sentences / words * 100)index = 0.0588 * L - 0.296 * S - 15.8roundedIndex = round(index)if roundedIndex < 1:    print("Before Grade 1")if roundedIndex >= 16:    print("Grade 16+")else:    print(roundedIndex)
查看完整描述

1 回答

?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

在您的循環中,您正在檢查整個字符串是按字母順序排列還是空格,而不是與索引對應的字母是否i是。你可能想要st[i].isalpha()和st[i].isspace().


或者,您可以直接遍歷字符,而不是遍歷索引:


for char in st:

    if char.isalpha():

        ...

    if char.isspace():

        ...


查看完整回答
反對 回復 2022-10-11
  • 1 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號