学校python项目需要一点帮助[关闭]

问题描述 投票:-1回答:2

所以,我有一个出售水果的项目,我们目前正在使用术语“用于”我使用了其他术语,但是我们不能为此使用“ while”。

[如果客户想要一个苹果,则苹果可享受20%的折扣。如果他们想要折扣40%的香蕉,还有一个30折的橙]

它们都花了50比索在我停止程序之前,客户是无限的,最后,我必须加上我赚了多少钱的总和我知道我可以写“ IF”来获得分辨率,

但是我的问题是,我可以用什么来圈出问题?我可以用什么术语来将所有事物的总和加到最后?

python new-operator
2个回答
0
投票

根据您的发言,通过while循环可以做到这一点

fruit=input('what fruit are you selling?(banana,orange or apple) ')
count=int(input('how many? '))
fruits=['orange','apple','banana']
total=0
while fruit in fruits:
    if fruit=='orange':
        total=total+count*0.7*50
    if fruit=='apple':
        total=total+count*0.8*50
    if fruit=='banana':
        total=total+count*0.6*50
    fruit=input('what fruit are you selling?(banana,orange or apple) ')
    count=int(input('how many? '))
print(total,'Pesos')

0
投票

我是否正确假设您希望不使用“ while”而是使用“ for”循环来解决此问题?这是一些不是很聪明的代码(如果您不按A B或C将会失败):

apple = 50
apple_discount = 0.2
banana = 50
banana_discount = 0.3
orange = 50
orange_discount = 0.4
total_money = 0

for i in range(0, 2):
    fruit = input("which fruit do you want (A), (B), (C)? ")

    ## chekc if input was A
    if fruit == "A": 
        price = apple - apple * apple_discount
        print("you bought an Apple for {} pesos".format(price))

    ## check if input was B
    if fruit == "B":
        price = banana - banana * banana_discount
        print("you bought a Banana for {} pesos".format(price))

    ## check if input was C
    if fruit == "C":
        price = orange - orange * orange_discount
        print("you bought an Orange for {} pesos".format(price))

    total_money += price
    print("total money to pay:", total_money)

    ## this makes the loop run one time longer every loop 
    ## (which is stupid) so it runs forever:
    i = i-1

我编写的代码尽可能地易于理解,希望它可以帮助您对此事产生更多的兴趣。

© www.soinside.com 2019 - 2024. All rights reserved.