Skip to main content
 首页 » 编程设计

python菜单,如何使这个菜单工作

2024年11月01日10shihaiming

def pickOne(): 
    print 'PROPERTIES OF SOLIDS OF REVOLUTION' 
    print 'Pick a solid to analyze' 
    print ' 1: ball\n' \ 
          ' 2: bwolingPin\n' \ 
          ' 3: ellipse\n ' \ 
          ' 4: tableLeg\n' \ 
          ' 5: quit\n\n ' 
    menu = [] 
    silhouette = () 
    flag = 1 
    while flag: 
        pickS = raw_input('What is the Number of your choice?') 
        pickS = int(pickS) 
        if pickS == 1: 
            silhouette = str(ball) 
            flag = 1 
        if pickS == 2: 
            silhouette = bowlingPin 
            flag = 1 
        if pickS == 3: 
            silhouette = ellipse 
            flag = 1 
        if pickS == 4: 
            silhouette = tableLeg 
            flag = 1 
        if pickS == 5: 
            flag = 1 
            main() 
        else: 
            flag = 0 
            print 'Choice %s is not a valid choice' %(pickS) 
            return 
        tempinput = raw_input('enter min and max number of points to use (e.g., 2 1000)').split(' ') 
        minNum = tempinput[0]; maxNum = tempinput[1] 
    return silhouette , minNum, maxNum 

我是编程新手, 听说要给一个程序做一个菜单,body代码我做完了,就是这个菜单实在是太难了,希望大家帮帮我。

我希望这个 fnc 的工作方式是,如果用户为第一个选项选择 1-4,它会继续并转到第二个确定,它将返回 2 到 1000 之间的两个 int。 整个函数将返回剪影的名称,以便其他 fnc 可以调用它。和两个诠释。

请您参考如下方法:

根据我对你问题的理解,你的逻辑是有缺陷的。首先,您使用 if 语句,其中您可能想使用 elif,它是 else if 的缩写。其次,由于您的标志设置,您的功能将始终以打印出“无效选择”结束。还有一个未使用的变量和一堆不必要的标志设置,您的 minnum 和 maxnum 变量是字符串而不是整数(列表理解可以解决这个问题)。您还可以执行称为序列解包的操作,以从 tempinput 中获取值。变量和函数名称通常写成 lower_case_with_underscores 而不是 mixedCase。

列表理解:https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions 序列解包:https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences

def pick_one(): 
    print '''PROPERTIES OF SOLIDS OF REVOLUTION 
    Pick a solid to analyse: 
    1: Ball 
    2: Bowling Pin 
    3: Ellipse 
    4: Table Leg''' 
 
    silhouette = None 
    choice = int(raw_input('Enter the number for your choice: ')) 
    while choice not in [1, 2, 3, 4]: 
        choice = int(raw_input('Enter the number for your choice: ')) 
 
    if choice == 1: 
        silhouette = ball 
    elif choice == 2: 
        silhouette = bowling_pin 
    elif choice == 3: 
        silhouette = ellipse 
    elif choice == 4: 
        silhouette = table_leg 
 
    tempinput = raw_input('Enter the minimum and maximum number of points to use (e.g. 2 1000) ').split() 
    min_num, max_num = [int(n) for n in tempinput] 
 
    return silhouette, min_num, max_num