Python 3.x 获取变量的最后一个已知值

Python 3.x 获取变量的最后一个已知值,python-3.x,Python 3.x,如果您为所有最终打印输入2,则最后一项的总花费为8,总花费为27 27是最后已知的总数 打印('花费的总金额为{},您有{}'。格式(总金额,货币)) 最后一行:我希望它总共打印27张。然而,事实并非如此。它正在打印最后购买的一套水果的总数,即8。您没有汇总所有水果的总数。当总数为4、6和8时,“花费”则为4、10(4+6)和18(4+6+8) 总数不是2*(2+3+4)=18吗?找到第一个子表达式,它是您可以显示做了您希望做的事情的代码,由不做您希望做的事情的代码扩展。(基本调试。)如果您还没

如果您为所有最终打印输入2,则最后一项的总花费为8,总花费为27

27是最后已知的总数

打印('花费的总金额为{},您有{}'。格式(总金额,货币))


最后一行:我希望它总共打印27张。然而,事实并非如此。它正在打印最后购买的一套水果的总数,即8。

您没有汇总所有水果的总数。当总数为4、6和8时,“花费”则为4、10(4+6)和18(4+6+8)


总数不是
2*(2+3+4)=18吗
?找到第一个子表达式,它是您可以显示做了您希望做的事情的代码,由不做您希望做的事情的代码扩展。(基本调试。)如果您还没有这样做,那么您还没有完成最小的调试研究。请参阅,其他链接,点击谷歌的“stackexchange家庭作业”和投票箭头鼠标悬停文本。然后将其扩展到--包括剪切、粘贴和可运行的代码(包括输入),以及所需的、实际的输出和清晰的说明和解释。然后解释你的期望&为什么。然后问一个简洁具体的问题,为什么你没有得到你想要的。
enter code here

fruits ={'apple':2,'banana':3,'orange':4}
money = 45
for fruit in fruits:
    print ('Each {} cost {} dollars'.format (fruit,fruits[fruit]))
    count = int(input("How many {} do you want?".format(fruit)))
    print ('You want {} {}s'.format (count,fruit))
    total = count * fruits[fruit]
    print ('The total price is {} Naira'.format(total))

    if money > total:

        print ('You have bought {} and have {} left'.format(total,money))
        money-=total
        \
        print ('Total money spent is {} and you have{}'.format(total,money))
fruits = {'apple':2, 'banana':3, 'orange':4}
money = 45
spent = 0
for fruit in fruits:
    print ('Each {} cost {} dollars'.format (fruit, fruits[fruit]))
    count = int(input("How many {} do you want?".format(fruit)))
    print ('You want {} {}s'.format (count, fruit))
    total = count * fruits[fruit]
    print ('The total price is {} Naira'.format(total))
    if money > total:
        print ('You have bought {} and have {} left'.format(total, money-total))
        money -= total
        spent += total
        print ('Total money spent is {} and you have{}'.format(spent, money))