2017-02-03 275 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