-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"
4

3 回答 3

4

几件事

  1. 你的变量被命名为列表,虽然这不是一个错误,但这是错误的形式
  2. 你的列表真的是一个元组,元组不能改变
  3. list.insert 不是有效的python
  4. inputraw_input在 python 2.x中很危险,请尝试
  5. 目标永远不会在“列表”中,因此你永远不会进入你的 while 循环
  6. 几个语法错误

input 将评估它给出的任何东西,甚至像import os;os.deltree("C:"); DONT TRY THIS 之类的东西!这将允许恶意用户在运行您的软件的系统上执行他们想要执行的任何操作

于 2013-04-23T17:11:27.653 回答
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
于 2014-06-13T13:02:01.333 回答
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
于 2014-06-13T13:36:56.393 回答