【问题标题】:Program not taking in prices properly程序没有正确考虑价格
【发布时间】:2019-11-22 09:31:34
【问题描述】:

嗨,我对 Python 很陌生,所以我猜我在这里犯了一个非常明显的错误,但基本上我想让我的代码在这里做的是从用户那里获取 5 种产品及其价格,产品正在工作,但是当用户输入高于 0 的价格时,会显示“请输入高于 0 的价格”的消息,这是我下面的代码,非常感谢对新手的任何帮助。

#Lists of products and prices

products = []
price = [] #declare lists
total = 0 #declare variable to hold total of prices

#function which is used to read in values to each list in turn 
#range has been set to read in 5 values
def inputPrice():
    for counter in range(0,5):
        valid = False
        print (counter+1, "Enter 5 Products:")
        tempItems = input() #declare temp variable to hold input value
        products.append(tempItems) #add this value into our list
            #when reading in price if statement is added for validation
        print (counter+1, "Enter their prices:")
        tempCost = int(input())
        if tempCost<=0: #validate input
               print ("Incorrect price....")
               print ("Please enter a price above 0")
        else: 
            valid = True
            price.append(tempCost)

【问题讨论】:

标签: python arrays input


【解决方案1】:

这里的主要关键是如果条件没有发生则循环

products = []
price = [] #declare lists
def inputPrice():
    for counter in range(1,6):
        print ("Enter Product {} out of 5".format(counter))
        tempItems = input() #declare temp variable to hold input value
        while tempItems == '': #validate input
            print ("Incorrect Product ...")
            print ("Please enter a valid product")
            tempItems = input()
            #when reading in price if statement is added for validation
        print ("Enter the price of Product {} out of 5".format(counter))
        tempCost = int(input())
        while tempCost <=0: #validate input
            print ("Incorrect price....")
            print ("Please enter a price above 0")
            tempCost = int(input())
        products.append(tempItems)
        price.append(tempCost)
    print('products' , products)
    print('price' , price)

inputPrice()

【讨论】:

    【解决方案2】:

    更新您的代码添加 while 循环以从用户那里获取价格

    产品和价格列表

    products = []
    price = [] #declare lists
    total = 0 #declare variable to hold total of prices
    
    #function which is used to read in values to each list in turn 
    #range has been set to read in 5 values
    def inputPrice():
        for counter in range(0,5):
            valid = False
            print (counter+1, "Enter 5 Products:")
            tempItems = input() #declare temp variable to hold input value
            products.append(tempItems) #add this value into our list
                #when reading in price if statement is added for validation
            while True:
              print (counter+1, "Enter their prices:")
              tempCost = int(input())
              if tempCost<=0: #validate input
                   print ("Incorrect price....")
                   print ("Please enter a price above 0")
              else:
                break
            else: 
                valid = True
                price.append(tempCost)
                
    inputPrice()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多