Python编程PTA题解大全——索引

Description:给定四种水果,分别是苹果(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。
Input:输入仅一行,连续输入的若干个编号。
Output:首先在屏幕上显示菜单。然后对应用户的每个输入,在一行中按格式“price = 价格”输出查询结果,其中价格保留两位小数。当用户连续查询次数超过5次、或主动输入0时,程序结束。
Sample Input:3 -1 0 2
Sample Output
[1] apple
[2] pear
[3] orange
[4] grape
[0] exit
price = 4.10
price = 0.00
Sample Input:1 2 3 3 4 4 5 6 7 8
Sample Output
[1] apple
[2] pear
[3] orange
[4] grape
[0] exit
price = 3.00
price = 2.50
price = 4.10
price = 4.10
price = 10.20

print("[1] apple")
print("[2] pear")
print("[3] orange")
print("[4] grape")
print("[0] exit")
price = input().split()
n = 5
for i in price:
    if n > 0:
        if int(i) == 0:
            exit(0)
        elif int(i) == 1:
            print("price = %.2f" % 3.00)
        elif int(i) == 2:
            print("price = %.2f" % 2.50)
        elif int(i) == 3:
            print("price = %.2f" % 4.10)
        elif int(i) == 4:
            print("price = %.2f" % 10.20)
        else:
            print("price = %.2f" % 0.00)
    n -= 1
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐