如何正确使用列表功能

时间:2017-06-20 07:26:11

标签: python list count

dnlist = ["Place 1","Place 2","Place 3"] 
polist = [] 
splist = [] 
dncount = 3 
count = 0 


def prices (): 
    while count <= dncount:
        print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        print("Please enter the details for ",dnlist[count])
        print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
    #Ask for the dp
    try:
        sp = int(input("the dp is $"))
        print(sp)
    except ValueError:#Make sure they only input numbers
        print("Sorry, you must put number only.")
    splist.append(sp)


    #Ask for the po                
    try:
        po = int(input("the po is %"))
    except:
        print("Please enter a valid number.")

    #This is to ensure that the customer enters a reasonable percentage 
    if po <=10: print("Your percentage is too low. Please enter a higher percentage.") 
    if po >=95: print("Your percentage is too high. Please enter a lower percentage.") 
    polist.append(po)

prices()

这个程序适用于1号地方,但它不会继续询问地方2和地方3的dp和po

当我运行该程序时,它不会停止要求输入Place 1的详细信息,但我希望我的程序以这种方式工作..

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Please enter the details for Place 1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
the dp is $21
21
the po price is %34
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Please enter the details for  Place 2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
the dp is $98
98
the po is %24
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Please enter the details for  Place 3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
the dp is $109
109
the po is %87

1 个答案:

答案 0 :(得分:0)

只是一个小修改版本,使其工作。但以这种方式使用count并不是很好。将它分配给函数内部的变量会更好

dnlist = ["Place 1","Place 2","Place 3"]
polist = []
splist = []
dncount = 3
count = 0


def prices():
    while count <= dncount:
        print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        print("Please enter the details for ",dnlist[count])
        print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        #Ask for the dp
        try:
            sp = int(input("the dp is $"))
            print(sp)
        except ValueError:#Make sure they only input numbers
            print("Sorry, you must put number only.")
        splist.append(sp)


        #Ask for the po
        try:
            po = int(input("the po is %"))
        except:
            print("Please enter a valid number.")

        #This is to ensure that the customer enters a reasonable percentage
        if po <=10: print("Your percentage is too low. Please enter a higher percentage.")
        if po >=95: print("Your percentage is too high. Please enter a lower percentage.")
        polist.append(po)
        global count
        count += 1

prices()

第二个版本,以避免警告count

dnlist = ["Place 1","Place 2","Place 3"]
polist = []
splist = []
dncount = 3
count = 0


def prices(count):
    while count < dncount:
        print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        print("Please enter the details for ",dnlist[count])
        print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
        #Ask for the dp
        try:
            sp = int(input("the dp is $"))
            print(sp)
        except ValueError:#Make sure they only input numbers
            print("Sorry, you must put number only.")
        splist.append(sp)


        #Ask for the po
        try:
            po = int(input("the po is %"))
        except:
            print("Please enter a valid number.")

        #This is to ensure that the customer enters a reasonable percentage
        if po <=10: print("Your percentage is too low. Please enter a higher percentage.")
        if po >=95: print("Your percentage is too high. Please enter a lower percentage.")
        count += 1

prices(count)