满世界溜达的貔貅 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日

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上