2017-02-03 220 views
0

我一直在为我的作业编写程序: 为每周工作五天的汽车销售人员编写程序。该计划应提示每天有多少辆汽车被售出,然后在当天提示每辆汽车的售价(如果有的话)。在输入所有五天的数据之后,该计划应报告该期间销售的汽车总数和总销售量。见示例输出。注意:重复显示总销售的货币格式,Python循环语句

示例输出 第1天售出了多少辆汽车? 1 汽车1的售价? 30000 第2天售出了多少辆汽车? 2 汽车1的售价? 35000 汽车2的售价? 45000 第3天售出了多少辆汽车? 0 第4天售出了多少辆汽车? 1 汽车1的售价? 30000 第5天售出了多少辆汽车? 0 您已销售4件汽车,总销售额为$ 140,000.00

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

这是我的代码,我也采取了基本的Python课程,所以我是新的!

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()

回答

0

为此,您可能需要使用嵌套的for循环来提示输入的每个车厢。

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