2013-04-23 3 views
-2

목록을 변경하기위한 옵션 집합을 보여주는 목록을 만들려고합니다. 하지만 작동하지 않는 사람이 나에게 무엇이 잘못되었는지 말해 줄 수 있습니다.간단한 파이썬 메뉴 만들기

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

+3

당신은 어떤'등 다양한 구문 오류가 있습니다'조건문 후, 당신은 할당 연산자를 ('사용하고 =을 ') 당신이 비교를 원할 때 ('=='), 등등. 파이썬에 대한 서적을 가지고 시작해야 할 것입니다. –

+1

파이썬에서이 코드가 있습니까? –

답변

4

몇 가지

당신의 변수가 목록에 이름
  1. 아닌 오류가 정확히이 나쁜 형태 동안
  2. 목록은 정말 튜플, 튜플은
  3. list.insert을 변경할 수 없습니다입니다
  4. 유효하지 않은 python
  5. input은 python 2.x에서 위험합니다. 대신 을 시도하십시오. 당신이 당신의 루프 동안
  6. 몇 가지 구문 오류

입력이 DONT는이 시도 import os;os.deltree("C:");같은 심지어 뭔가를 주어 어떤 평가합니다를 입력하지 않습니다 그 때문에대상 "목록"에 없을 것! 이것은 악의적 인 사용자가 소프트웨어를 실행하는 시스템에서 원하는 것을 실행할 수있게합니다.

+0

는 숫자 4를 설명 할 수 있습니다. @Joran Basley – user2312139

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