2016-11-04 20 views
0
studentList=[] 

reading=True 

readList=open("test10.4.3.txt","r") 

for item in readList: 
    studentList.append(item) 
for item in studentList: 
    print(item) 
input("\nPress ENTER") 

menulist=print("1. Print the list", 
     "2. Add a name to the list", 
     "3. Remove a name from the list", 
     "4. Change an item in the list", 
     "9. Quit") 

def menu(): 
    aMenu=input("please select a number") 
    return aMenu 
t=True 
while t: 
    target=menu() 
    if target=="1": 
     print(studentList) 
    if target=="2": 
     Addname=input("Type in a name and major to add:") 
     list=list.append(Addname) 
     print(menulist) 
    if target=="3": 
     Removename=input("What name and major would you like to remove:") 
     list=list.remove(Removename) 
     print(menulist) 
    if target=="4": 
     Changename=input("What name and major would you like to change:") 
     changetoname=input("What is the new name and major:") 
     list=list.replace(Changename, changetoname) 
     print (menulist) 

    if target=="9": 
     t=False 
     print("good bye") 
    else: 
     print (menulist) 

私はtxtファイルに格納されているリストを変更しようとしています。 txtファイルの中には3つの名前とその3人の学術系専攻があります。私は変更を加えることができるはずのプログラムを書いています(私はこれを非常に新しく考えています)。しかし、私は、入力2名を追加するには、主要な、それは私にtxtファイルに格納されているリストを変更するにはどうすればよいですか?

Traceback (most recent call last): 
    File "D:/py/Scripting/class.py", line 52, in <module> 
    list=list.append(Addname) 
TypeError: descriptor 'append' requires a 'list' object but received a 'str' 

Process finished with exit code 1 

を与えるか、私は入力3名を削除するとき、それは私に与え、私は入力4私が手

Traceback (most recent call last): 
    File "D:/py/Scripting/class.py", line 56, in <module> 
    list=list.remove(Removename) 
TypeError: descriptor 'remove' requires a 'list' object but received a 'str' 

Traceback (most recent call last): 
    File "D:/py/Scripting/class.py", line 56, in <module> 
    list=list.remove(Removename) 
TypeError: descriptor 'remove' requires a 'list' object but received a 'str' 

あなたはeを見ることができますエラーはより少なくなります。私は何が間違っているのか分かりません。任意のアイデアや例をいただければ幸いです。

+2

変数として 'list'を使用せず、append/removeがインプレースです。あなたのリストを破壊する結果を割り当てないでください。 –

+0

'print'は' None'を返します –

+0

あなたはそこにあるものをいくつかクリーンアップする必要があるかもしれません。 'studentList'は一度だけ読み込まれますが、項目を追加/削除するときは参照しません。そして、変数名として 'list'を使わないでください。 – sal

答えて

0

ほとんどのエラーを修正し、コード内にコメントとしてコメントと説明を追加しました。

studentList=[] 
# This variable doesn't appear to be used anywhere 
reading=True 

readList=open("test10.4.3.txt","r") 

for item in readList: 
    # I added a call to the .strip() method for the item which removes 
    # whitespace in front of and at the end of the string. Effectively 
    # this removes the linebreak from the file 
    studentList.append(item.strip()) 
for item in studentList: 
    print(item) 
input("\nPress ENTER") 

# I reformated the output of the menu a little bit, it is now a string 
# with a few linebreaks (\n) --> You might want to call this variable menu 
# now as it is no longer a list (although it wasn't a list before) 
menulist="1. Print the list\n2. Add a name to the list\n3. Remove a name from the list\n4. Change an item in the list\n9. Quit" 
# print should be a separate statement 
print(menulist) 

def menu(): 
    # I added a space after number, which looks better in the output 
    # (In my opinion) --> Gap between your text and the thing the user enters 
    aMenu=input("please select a number ") 
    return aMenu 

t=True 
while t: 
    target=menu() 
    if target=="1": 
     print(studentList) 
    if target=="2": 
     # Added a space again 
     Addname=input("Type in a name and major to add: ") 
     # You want to append it the the studentList 
     # --> you need to call append on that list 
     studentList.append(Addname) 
     # This print statement is redundant, since the menu will be 
     # printed by the else case of the if target=="9" conditions 
     # --> The menu will be printed twice 
     print(menulist) 
    if target=="3": 
     # Added a space again 
     Removename=input("What name and major would you like to remove: ") 
     # You want to remove the name from the studentList 
     # --> you need to call remove on that list 
     studentList.remove(Removename) 
     # This print statement is redundant, since the menu will be 
     # printed by the else case of the if target=="9" conditions 
     # --> The menu will be printed twice 
     print(menulist) 
    if target=="4": 
     # Added spaces again 
     Changename=input("What name and major would you like to change: ") 
     changetoname=input("What is the new name and major: ") 
     # You want to replace values inside of the studentList 
     # --> you would need to call some sort of replace function on that list 
     #  unfortunately lists don't actually have a replace function 
     # --> I don't want to cheat you out of the learning experience, you 
     #  already used functions which would allow you to achieve a replace-like 
     #  behaviour. 
     # studentList.replace(Changename, changetoname) 

     # This print statement is redundant, since the menu will be 
     # printed by the else case of the if target=="9" conditions 
     # --> The menu will be printed twice 
     print (menulist) 

    if target=="9": 
     t=False 
     print("good bye") 
    else: 
     print (menulist) 

あなたは明らかに初心者なので、私はコーディングスタイルにコメントしませんでした。彼らは異なり、あなたのコードの流れを制御するために組み合わせて使用​​することができる方法(docs) - しかし、私はあなたが次の概念

  • ifelifelseのいくつかを見てお勧めします。

  • lists pythonで - 実際にどのように動作しているのか誤解されているようですが、うまくいけばdocsはこれをクリアすることができます。

  • docsのファイルを読み書きする。

注:私はコード内のコメントで指摘したように、リストはreplace機能を持っていません。私はこのような行動をどのように達成することができるのかを明らかにしませんでした。なぜなら、これは自分自身を把握できなければならないものです。

+0

ありがとうございます!しかし私は、名前とメジャーが保存されているtxtファイルに対して、私が行った変更や追加のどれも行われていないことに気付きました。それがなぜ起こっているのか、それをどうやって修正できるのかというアイデアはどうですか? –

+0

はい、あなたはファイルからのみ読み込み中で、変更を書き戻すことはありません。ファイルを読み書きするための最後の[link](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files)を見てください。これはPythonのドキュメントですそれを行う方法のいくつかの例を挙げてください。 – Maurice

+0

もし私がreadList = open( "test10.4.3.txt"、 "r +")をrの代わりに変更しても、r +はドキュメントを読んで書いているだけなので、読み書きできますか? –

関連する問題