代码如下:
price={"apple":5,"peach":6,"banana":3,"pear":4}
print("today's fruit price")
furit=int()
num=int()
for furit in price:
print("%s %d yuan/jin"%(furit,price[furit]))
print("")
n=int(input("pls input what many types of fruits you want to buy:"))
sum_price=0
for i in range(0,n):
fruit=input("input the name of fruit%d:"%(i+1))
if fruit in price:
sum_price+=price[fruit]*num
num=int(input("input the quantity%d of fruit:"%(i+1)))
else:
print("the %s is not on-sell"%(furit))
print("total price is %d"%(sum_price))
输入orange之后,应该是the orange is not on-sell, 但却打印出pear:
today's fruit price
apple 5 yuan/jin
peach 6 yuan/jin
banana 3 yuan/jin
pear 4 yuan/jin
pls input what many types of fruits you want to buy:4
input the name of fruit1:apple
input the quantity1 of fruit:1
input the name of fruit2:orange
the pear is not on-sell
input the name of fruit3:
这都是你自己整的。furit和fruit,两个变量名你搞错了。因为前面那个for,现在的furit 变量是pear,print("the %s is not on-sell"%(furit))应该填的是fruit而不是furit,填了furit就导致输出了pear。改成print("the %s is not on-sell"%(fruit))就好了。
下次别乱起变量名了
贴一下代码块,以及报错截图
price={"apple":5,"peach":6,"banana":3,"pear":4}
print("today's fruit price")
furit=int()
num=int()
for furit in price:
print("%s %d yuan/jin"%(furit,price[furit]))
print("")
n=int(input("pls input what many types of fruits you want to buy:"))
sum_price=0
for i in range(0,n):
fruit=input("input the name of fruit%d:"%(i+1))
if fruit in price:
sum_price+=price[fruit]*num
num=int(input("input the quantity%d of fruit:"%(i+1)))
else:
print("the {} is not on-sell".format(fruit))
print("total price is %d"%(sum_price))
望采纳!谢谢