def fruit_price(fruit): if fruit == 'Watermelon': return 1.6 elif fruit == 'Apple': return 10 elif fruit == 'Peach': return 3.3 elif fruit == 'Pear': return 2 else: return None
def enter_price(): price = [] weights = [] fruits = [] num = 0 while True: num += 1 fruit = input('Enter the fruit: ') weight = input('Enter the weight: ') fruits.append(fruit) weights.append(weight) a = fruit_price(fruit) price.append(a) if fruit == 'q' or weight == 'q': break num -= 1 fruits.remove(fruits[-1]) price.remove(price[-1]) weights.remove(weights[-1])
# 循环打印水果,单价,数量 num = fruit_res[0] i = 0 prices = [] for i in range(int(num)): prices.append(int(fruit_res[2][i]) * float(fruit_res[3][i])) print(fmt.format(fruit_res[1][i], fruit_res[2][i], fruit_res[3][i])) i += 1
Enter the fruit: Watermelon Enter the weight: 10 Enter the fruit: Apple Enter the weight: 20 Enter the fruit: Peach Enter the weight: 30 Enter the fruit: q Enter the weight: q [3, ['Watermelon', 'Apple', 'Peach'], ['10', '20', '30'], [1.6, 10, 3.3]] Please enter your width: 45 ********************************************* Here's Your Ticket ============================================= Item Num Price --------------------------------------------- Watermelon 10 1.60 Apple 20 10.00 Peach 30 3.30 --------------------------------------------- Summary 315.00 ---------------------------------------------