2017-02-07 7 views
-1

私は以下のようにファイルに何かを書いています。条件を真とし、引数として渡します。

以下のようにこの機能が他の関数を呼び出すされ
def func(arg1,arg2,arg3): 
    lin=open(file1,"r") 
    lin1=open(file,"w") 
    a=lin.readline().strip('\n') 
    lin1.write(a + " " + id) 
    lin.close() 
    lin1.close() 

lin1.write(a + " " + id + " " + y/n) 

が、そのY/Nべき:私はとLIN1を書き込み中に別の引数を追加する必要がありますしたい

def afunc(arg1,arg2): 
    doing stuff 
    arg1.func(arg2,arg3) 

ユーザーの入力から来ます。そして、このユーザ入力はafucn()

例第二の機能に言及する必要があります:

res = str(raw_input('Do you want to execute this ? (y/n)')) 

私はyはnが引数としてLIN1に追加する必要があり、それをLIN1とあれば、プレスnを追加する必要がありYを押した場合。

+0

のように実装することができますか? – MYGz

+0

@MYGz試しました。しかし、 "Doing stuff"のすぐ上の2番目の機能でそのユーザーの応答が必要です。可能であればお勧めします。 – Srinivas

+1

[mcve]を作成できますか?完全なコードを1ブロックに入れる。あなたが話している2つのケースの入力と出力を含めますか? – MYGz

答えて

0

コードを確認して、役に立つヒントを表示しようとしています。私はあなたが初心者であることを知っています。

def func(arg1,arg2,arg3): # Make sure that you use the same names here 
    # as in your actual code to avoid confusions - simplification is not a point here, 
    # especially if you are a novice 
    # also, always try to choose helpful function and variable names 
    lin=open(file1,"r") # use with context manager instead (it will automatically close your file) 
    lin1=open(file,"w") 
    a=lin.readline().strip('\n') # it reads only one line of you file, 
    # so use for loop (like - for line in f.readlines():) 
    lin1.write(a + " " + id) # it rewrites entire file, 
    # better to collect data in some python list first 
    lin.close() 
    lin1.close() 

def afunc(arg1,arg2): 
     # doing stuff # use valid python comment syntax even in examples 
     arg1.func(arg2,arg3) # func is not a method of some python Object, 
     # so it should be called as just func(arg1, arg2, arg3) 
     # when your example won't work at all 

そして、あなたのタスクはただ、書き込む前に `if`条件を入れ

def fill_with_ids(id_list, input_file, output_file): 
    with open(input_file) as inp: 
     input_lines = inp.readlines() 
    output_lines = [] 
    for i, line in enumerate(input_lines): 
     output_lines.append(line + ' ' + id_list[i]) 
    with open(output_file, 'w') as outp: 
     outp.writelines(output_lines) 


def launch(input_file, output_file): 
    confirm = input('Do you want to execute this? (y/n)') 
    if confirm != 'y': 
     print 'Not confirmed. Execution terminated.' 
     return 
    id_list = ['id1', 'id2', 'id3'] # or anyhow evaluate the list 
    fill_with_ids(id_list, input_file, output_file) 
    print 'Processed.' 


def main(): 
    launch('input.txt', 'output.txt') 


if __name__ == '__main__': 
    main() 
関連する問題