第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

為什么我只得到“0”輸出?

為什么我只得到“0”輸出?

白豬掌柜的 2024-01-27 16:05:19
我試圖搜索文本文件并確定文本文件中有多少行、元音、輔音和數(shù)值/字符。我的元音、輔音和數(shù)字輸出值似乎沒(méi)有正確計(jì)算。線路輸出總計(jì)已正確計(jì)算。ein = input("Please enter file name: ")vowels = set("AEIOUaeoiu")cons = set("BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwvyz")num = set("1234567890")count = 0Vcount = 0Ccount = 0Ncount = 0with open(ein) as ein_handle:    for line in ein_handle:        count += 1    for line in ein_handle:        if line in vowels:            Vcount += 1        elif line in cons:            Ccount += 1        elif line in num:            Ncount += 1print("the file has",count,"lines.")print("the file has",Vcount,"vowels.")print("the file has",Ccount,"consonants.")print("the file has",Ncount,"numerical characters.")
查看完整描述

2 回答

?
一只萌萌小番薯

TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊

第二個(gè)循環(huán)應(yīng)該針對(duì)給定行中的每個(gè)字符。


試試這個(gè):


ein = input("Please enter file name: ")

vowels = set("AEIOUaeoiu")

cons = set("BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwvyz")

num = set("1234567890")

count = 0

Vcount = 0

Ccount = 0

Ncount = 0

with open(ein) as ein_handle:

    for line in ein_handle:

        count += 1

        for letter in line:

            if letter in vowels:

                Vcount += 1

            elif letter in cons:

                Ccount += 1

            elif letter in num:

                Ncount += 1

print("the file has", count, "lines.")

print("the file has", Vcount, "vowels.")

print("the file has", Ccount, "consonants.")

print("the file has", Ncount, "numerical characters.")


另外,在第二個(gè)循環(huán)內(nèi)得到 0 (而不是行數(shù))的主要原因是因?yàn)楫?dāng)?shù)谝粋€(gè)循環(huán)執(zhí)行時(shí),它會(huì)在每次迭代時(shí)更新讀取偏移位置,直到它結(jié)束。因此,當(dāng)?shù)诙€(gè)循環(huán)開(kāi)始時(shí),它從文件末尾開(kāi)始,無(wú)法讀取任何內(nèi)容。


查看完整回答
反對(duì) 回復(fù) 2024-01-27
?
慕碼人8056858

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊

這是另一種方法:


import os

d,_ = os.path.split(__file__)


ein = input("Please enter file name: ")

vowels = "aeoiu"

cons = "bcdfghjklmnpqrstvwvyz"

num = "1234567890"

Vcount = 0

Ccount = 0

Ncount = 0

with open(d+"\\" + ein) as ein_handle:

    lines = ein_handle.readlines()

    for line in lines:

        line = line.lower()

        for v in vowels:

            tmp = line

            while v in tmp:

                Vcount += 1

                tmp = tmp[tmp.find(v)+1:]

        for c in cons:

            tmp = line

            while c in tmp:

                Ccount += 1

                tmp = tmp[tmp.find(c)+1:]

        for n in num:

            tmp = line

            while n in tmp:

                Ncount += 1

                tmp = tmp[tmp.find(n)+1:]

print("the file has",len(lines),"lines.")

print("the file has",Vcount,"vowels.")

print("the file has",Ccount,"consonants.")

print("the file has",Ncount,"numerical characters.")


查看完整回答
反對(duì) 回復(fù) 2024-01-27
  • 2 回答
  • 0 關(guān)注
  • 262 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)