2013-04-23 197 views
-2

我試圖創建一個顯示你一組選項做更改列表清單。 但它不工作誰能告訴我什麼是錯的吧。創建一個簡單的Python菜單

menulist=("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") 

list=("johny","tom","kim","tim","jim") 

target=input("Pick an item from the menu:") 
while (target in list): 
    if target="1" 
     print list 
    elif target="2" 
     Addname=input("Type in a name to add:") 
     list=list.insert(Addname) 
      print menulist() 
    elif target="3" 
     Removename=input("What name would you like to remove:") 
     list=list.remove(Removename) 
      print menulist() 
    elif target="4" 
     Changename=input(What name would you like to change:") 
     changetoname=input("What is the new name:") 
     list=list.replace('Changename','changetoname') 
      print menulist() 
    elif target="9" 
      print"good bye" 
+1

你有沒有開放「在'elif的目標=」 4" '分支 – akluth 2013-04-23 16:59:49

+3

你有各種各樣的語法錯誤,比如沒有':'條件語句後,您使用的賦值運算符('= ')當你想比較(''==),等等。你或許應該入門書到Python – 2013-04-23 17:00:16

+1

開始在Python? – 2013-04-23 17:02:19

回答

4

幾件事情

  1. 你的變量命名爲列表,而不是一個錯誤究竟這是不好的形式
  2. 列表確實是一個元組,元組不能改變
  3. list.insert是無效的python
  4. input在python 2.x中是危險的,請嘗試raw_input而不是
  5. 目標將永遠不會在「清單」爲此你將永遠不會進入while循環
  6. 幾個語法錯誤

輸入將評估任何它被賦予甚至像import os;os.deltree("C:");DONT試試這個!這將允許惡意用戶運行軟件

+0

代碼u能解釋4號請@Joran Basley – user2312139 2013-04-23 17:20:57

1

在系統上執行任何他們想這是我在Python簡單的菜單的例子。它是來自這個網站的舊版本的改進版本。

import os 
import msvcrt as m 

# Function for waiting for key press 
def wait(): 
    m.getch() 

# Clear screen before to show menu, cls is MS Windows command 
os.system('cls') 

ans=True 
while ans: 
    print(""" 
    Simple menu: 
    ------------ 

    1.Add a Student 
    2.Delete a Student 
    3.Look Up Student Record 
    4.Exit/Quit 
    """) 
    ans=input("What would you like to do? ") 
    if ans=="1": 
     print("\nStudent Added") 
     print("\nPress Enter...") 
     wait() 
     os.system('cls') 
    elif ans=="2": 
     print("\nStudent Deleted") 
     print("\nPress Enter...") 
     wait() 
     os.system('cls') 
    elif ans=="3": 
     print("\nStudent Record Found") 
     print("\nPress Enter...") 
     wait() 
     os.system('cls') 
    elif ans=="4": 
     print("\nGoodbye") 
     ans = None 
    else: 
     print("\nNot Valid Choice Try again") 
     print("\nPress Enter...") 
     wait() 
     os.system('cls') 
     ans = True 
0

修復和評論,他們主要是語法錯誤。

menulist= '''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''' #assuming you want to display menulist, having it as a tuple is useless 

lst=("johny","tom","kim","tim","jim") #don't use reserved names for variables, may mess up things 

target=raw_input("Pick an item from the menu:") 

if target=="1": #this is an equality operator, whereas = is used to assign a variable (This checks the equality basically) 
    print lst 

elif target=="2": 
    Addname=raw_input("Type in a name to add:") 
    list=list.append(Addname) #use append instead of insert, insert is for a specific position in list 
    print menulist #no parentheses, menulist is not a function; also this doesn't have to be indented 

elif target=="3": 
    Removename=raw_input("What name would you like to remove:") 
    list=list.remove(Removename) 
    print menulist #again, I took the parentheses away 

elif target=="4": 
    Changename=raw_input("What name would you like to change:") #you'd missed the " at the beginning 
    changetoname=raw_input("What is the new name:") 
    list=list.replace(Changename, changetoname) #removed the '. They're the variables, not the strings 'Changename' etc that you want to replace. 
    print menulist 

elif target=="9": 
    print"good bye" #excessive indenting 

else: #this replaces the initial while 
    #do nothing if the initial input is not 1,2,3,4 or 9 
    print menulist