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

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

從文件返回詳細(xì)信息,python

從文件返回詳細(xì)信息,python

慕碼人8056858 2021-03-07 17:20:40
我有這段代碼,在這里我要計(jì)算的數(shù)量:.py腳本中的代碼行for_loops(“ for”)-while_loops(“ while”)if_statements(“ if”)函數(shù)定義(“ def”)乘法符號(hào)(“ *”劃分標(biāo)志(“ /”加號(hào)(“ +”)減號(hào)(“-”)在數(shù)學(xué)符號(hào)上,代碼有效,但是當(dāng)代碼正在尋找if語句返回2時(shí),則存在一個(gè),這是主要問題,但是這讓我覺得我寫錯(cuò)了for循環(huán),這可能會(huì)帶來更多錯(cuò)誤以后出現(xiàn)問題。除此之外,我不確定如何打印顯示為[]而不是Author name的Author行代碼:from collections import CounterFOR_=0WHILE_=0IF_=0DEF_=0x =input("Enter file or directory: ")print ("Enter file or directory: {0}".format(x))print ("Filename {0:>20}".format(x))b= open(x)c=b.readlines()d=b.readlines(2)print ("Author {0:<18}".format(d))print ("lines_of_code {0:>8}".format((len (c))))counter = Counter(str(c))for line in c:    if  ("for ") in line:        FOR_+=1        print ("for_loops {0:>12}".format((FOR_)))for line in c:    if  ("while ") in line:        WHILE_+=1        print ("while_loops {0:>10}".format((WHILE_)))for line in c:    if  ("if ") in line:        IF_+=1        a=IF_        print ("if_statements {0:>8}".format((a)))for line in c:    if  ("def ") in line:        DEF_+=1        print ("function_definitions {0}".format((DEF_)))print ("multiplications {0:>6}".format((counter['*'])))print ("divisions {0:>12}".format((counter['/'])))print ("additions {0:>12}".format((counter['+'])))print ("subtractions {0:>9}".format((counter['-'])))正在從以下文件讀取文件:'''DumboAuthor: Hector McTavish'''    for for for  # Should count as 1 for statementwhile_im_alive # Shouldn't count as a whilewhile blah # But this one should  if defined # Should be an if but not a def  def if # Should be a def but not an if    x = (2 * 3) + 4 * 2 * 7 / 1 - 2  # Various operators任何幫助將非常感激
查看完整描述

2 回答

?
長(zhǎng)風(fēng)秋雁

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

不用將源代碼視為字符串,而是使用ast模塊來解析源代碼,然后遍歷各個(gè)節(jié)點(diǎn):


import ast

from collections import Counter


tree = ast.parse('''

"""

Author: Nobody

"""


def foo(*args, **kwargs):

    for i in range(10):

        if i != 2**2:

            print(i * 2 * 3 * 2)


def bar():

    pass

''')


counts = Counter(node.__class__ for node in ast.walk(tree))


print('The docstring says:', repr(ast.get_docstring(tree)))

print('You have', counts[ast.Mult], 'multiplication signs.')

print('You have', counts[ast.FunctionDef], 'function definitions.')

print('You have', counts[ast.If], 'if statements.')

它非常簡(jiǎn)單,可以處理所有極端情況:


The docstring says: 'Author: Nobody'

You have 3 multiplication signs.

You have 2 function definitions.

You have 1 if statements.


查看完整回答
反對(duì) 回復(fù) 2021-03-27
?
GCT1015

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

if  ("if ") in line也算在內(nèi)def if #。


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

添加回答

舉報(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)