2016-10-29 4 views
0

Pythonのチャレンジ25Pythonの挑戦25:書き込みファイル

放課後のクラブのためにサインアッププログラムを書きます。それは次の詳細をユーザーに尋ねると、ファイルに保存する必要があります

  • 名姓が
  • は、上記の課題については性別
  • フォーム

私が書いた

  • 次のPythonコード:

    # Python Challenge 25 
    
    print ("Hello user, this is a virtual application form" 
         "\nfor joining after-school clubs at -Insert school name here-") 
    
    first_name = input("\n\nPlease input your first name:") 
    last_name = input("Please input your last name:") 
    gender = input("Please input your gender:") 
    form = input("Please input your form name:") 
    club = input("What after-school club would you like to attend?\n") 
    
    file = open("application-form.txt", "w") 
    file.write(first_name) 
    file.write (last_name) 
    file.write (gender) 
    file.write (form) 
    file.write (club) 
    file.close() 
    
    
    print (first_name, "Thank you for taking your time to fill this virtual form" 
         "\nall the information has been stored in a file to maintain confidentiality") 
    

    例o上記のコードの結果F:

    enter image description here

    私の質問

    テキストファイルは、すべてのユーザー入力を保存する
    1. が1行に格納され、私の方法があります各入力を別々の行に置くことができますか?

    2. 上記のコードを書くより効率的な方法はありますか?

  • +0

    より効率的にすると、短いコードを意味するのか、より速く実行するのでしょうか? –

    +0

    より短いコード。 – Hackers45

    答えて

    2

    printとは逆に、writeは自動的に行末を追加しません。書き込みの間にfile.write("\n")を追加して、行末を挿入することができます。

    また、joinを使用して、行の終わりを散在させた単一の文字列を作成し、その単一の文字列を書き込むことができます。

    例:コードより効率的な/すっきりを作るためのよう

    file.write("\n".join([line1, line2, line3])) 
    
    +0

    ありがとうございます! – Hackers45

    +0

    あなたのコードは私が直面していた問題を解決しました非常にありがとう! – Hackers45

    2

    1)file.write(first_name + '\n')file.write(first_name)を交換するか、直後の行file.write('\n')を追加します。

    2)私は、コードを高速に実行させることができるとは思わない、と私はそれが必要だと思いませんが、コードの品質/長さの面で私はこのようなファイル書き込み部を記述します。

    with open("application-form.txt", "w") as f: 
        for item in [first_name, last_name, gender, form, club]: 
         f.write(first_name + '\n) 
    
    +0

    @ Alex Hallあなたのお手伝いをしていただきありがとうございます。上記のコードは助けになりました! – Hackers45

    1

    、あなたがリクエストに関数を使用して、データを格納することができ:

    def get_info(prompts): 
        file = open("application-form.txt", "w") 
        for prompt in prompts: 
         file.write(input(prompt)) 
         file.write('\n') 
    
    
    print ("Hello user, this is a virtual application form" 
         "\nfor joining after-school clubs at -Insert school name here-") 
    
    prompts = ["\n\nPlease input your first name:", 
          "Please input your last name:", 
          "Please input your gender:", 
          "Please input your form name:", 
          "What after-school club would you like to attend?\n"] 
    
    get_info(prompts) 
    
    
    print ("Thank you for taking your time to fill this virtual form" 
         "\nall the information has been stored in a file to maintain confidentiality") 
    

    は、残念ながら私が取らなければならなかったこれを行うにはありがとうございました(最後の印刷声明)first_name電話をかけてください。もしあなたが絶対に必要としているのであれば、これまでに持っていたものを使うほうが良いでしょう。

    +0

    コードはもっと見やすくなりました。ご協力ありがとうございました。 – Hackers45

    関連する問題