首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python循环语句

Python循环语句
EN

Stack Overflow用户
提问于 2017-02-03 13:20:45
回答 1查看 688关注 0票数 0

我一直在为我的家庭作业编写一个程序:为一个一周工作五天的汽车销售人员写一个程序。该程序应该提示每天售出多少辆汽车,然后提示当天每辆汽车的售价(如果有的话)。在输入所有五天的数据后,程序应该报告这段时间内售出的汽车总数和总销量。请参见示例输出。注:复制销售总额显示的币种格式。

示例输出第1天售出多少辆汽车?1汽车1的售价? 30000第2天售出多少辆汽车? 2汽车1的售价? 35000汽车2的售价? 45000第3天售出多少辆汽车?0第4天售出多少辆汽车?1汽车1的售价1? 30000第5天售出多少辆汽车?0你售出4辆汽车,总销售额为140,000美元

我确实有一些代码我已经工作过,但我被卡住了。我可以弄清楚如何让程序提示用户在第二天卖出了多少辆汽车,等等。任何帮助都将不胜感激!

这是我的代码,我也在学习一门基本的python课程,所以我对此还是个新手!

代码语言:javascript
复制
    def main () :

   cars_sold = []
   num_days = int(input('How many days do you have sales?'))

   for count in range(1, num_days + 1):
       cars = int(input('How many cars were sold on day?' + \
                        str(count) + ' '))

   while (cars != cars_sold):
    for count in range(1, cars + 1):
        cars_sold = int(input('Selling price of car' ' ' + \
                           str(count) + ' '))

main ()

EN

回答 1

Stack Overflow用户

发布于 2017-02-03 13:50:56

为此,您可能希望使用嵌套的For循环来提示输入的每一辆汽车。

代码语言:javascript
复制
def main():
    cars_sold = 0
    total = 0
    num_days = int(input('How many days do you have sales? '))

    # for each day
    for i in range(1, num_days + 1):
        num_cars = int(input('How many cars were sold on day {0}? '.format(i)))
        cars_sold += num_cars

        # for each car of each day
        for j in range(1, num_cars + 1):
            price = int(input('Selling price of car {0}? '.format(j)))
            total += price

    # Output number of cars and total sales with $ and comma format to 2 decimal places
    print('You sold {0} cars for total sales of ${1:,.2f}'.format(cars_sold, total))

# Output
>>> main()
How many days do you have sales? 5
How many cars were sold on day 1? 1
Selling price of car 1? 30000
How many cars were sold on day 2? 2
Selling price of car 1? 35000
Selling price of car 2? 45000
How many cars were sold on day 3? 0
How many cars were sold on day 4? 1
Selling price of car 1? 30000
How many cars were sold on day 5? 0
You sold 4 cars for total sales of $140,000.00
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42017304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档