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

时间:2014-04-07 03:24:07

标签: python menu

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

我是编程新手, 我被告知要为一个程序做一个菜单,我完成了身体代码,只是这个菜单真的给了我很多时间,希望你们能帮助我。

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

2 个答案:

答案 0 :(得分:1)

如果我正确理解了这个问题,我会发现您的代码存在一些问题。如果您想使用if的缩写elif,则可以使用else if。除非您设置pickS == 5这是更新的代码,否则每次都会运行“选择无效”选项的方式。

 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 = ball
             flag = 1
         elif pickS == 2:
             silhouette = bowlingPin
             flag = 1
         elif pickS == 3:
             silhouette = ellipse
             flag = 1
         elif pickS == 4:
             silhouette = tableLeg
             flag = 1
         elif 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

答案 1 :(得分:1)

从我对你的问题的理解来看,你的逻辑存在缺陷。首先,您使用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