Bootstrap

【PTA-python】第4章-9 查询水果价格 (15 分)

第4章-9 查询水果价格

分析

建立起价格列表。建立起输入列表。对输入数据,输出在价格列表中的对应值,注意特殊情况的输出,以及标记位记录输入数据的个数。

题目

给定四种水果,分别是苹果(apple)、梨(pear)、桔子(orange)、葡萄(grape),单价分别对应为3.00元/公斤、2.50元/公斤、4.10元/公斤、10.20元/公斤。

首先在屏幕上显示以下菜单:

[1] apple
[2] pear
[3] orange
[4] grape
[0] exit

用户可以输入编号1~4查询对应水果的单价。当连续查询次数超过5次时,程序应自动退出查询;不到5次而用户输入0即退出;输入其他编号,显示价格为0。

解法

lst=list(map(int,input().split()))
n=len(lst)
price_list=[3.00,2.50,4.10,10.20]
cnt=0
print("[1] apple\n[2] pear\n[3] orange\n[4] grape\n[0] exit")
for i in range(n):
    if lst[i]==0:
        exit(0)
    if lst[i]<0 or lst[i]>4:
        print("price = 0.00")
        cnt+=1
        continue
    cnt+=1
    if cnt<=5:
        print("price = {:.2f}".format(price_list[lst[i]-1]))
    else:
        exit(0)
;