满世界溜达的貔貅 2022-04-25 10:38 采纳率: 100%
浏览 31
已结题

关于#pythonfor#的问题,如何解决?

代码如下:
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:

  • 写回答

3条回答 默认 最新

  • A Python 萌新花花 2022-04-25 10:45
    关注

    这都是你自己整的。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))就好了。
    下次别乱起变量名了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月3日
  • 已采纳回答 4月25日
  • 创建了问题 4月25日

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算