2017-10-08 16 views
-1

該程序將詢問用戶單價和數量。然後它將單位價格乘以數量。如果數量大於10,可享受5%的折扣。如果數量大於100,可享受10%的折扣。如何創建一個while循環來詢問用戶是否要繼續,如果不是,它會中斷?

def bmth(unit, quan): 
    def computeN(unit, quan): 
     z = unit * quan 
     print(z) 

    if 10 <= quan < 99: 
     ordercost = unit * quan 
     discfive = ordercost * .05 
     v = ordercost - discfive 
     print('Your original price was ', ordercost) 
     print('You Saved', discfive, 'off') 
     print('Your new price is now ', v) 
    elif quan >= 100: 
     ordercost2 = unit * quan 
     discten = ordercost2 * .10 
     n = ordercost2 - discten 
     print('Your original price was ', ordercost2) 
     print('You save ', discten, 'off') 
     print('Your new price is now ', n) 
    else: 
     computeN(unit, quan) 

while True: 
    unit = float(input('Enter the Unit price: ')) 
    quan = int(input('Enter your items: ')) 
    onelasttime = input('Do you wish to continue? Y or N: ') 
    Y = bmth(unit, quan) 
    if onelasttime: 
     input(Y) 
    elif onelasttime: 
     input(N) 
    break 

回答

0

固定的while循環:

while True: 
    unit = float(input('Enter the Unit price: ')) 
    quan = int(input('Enter your items: ')) 
    not_continue = input('Do you wish to continue? Y or N: ') is 'N' 
    bmth(unit, quan) 
    if not_continue: 
     break 

觀察到:

  1. onelasttime是一個字符串,所以'N'truthy和評估爲True
  2. bmth不返回,所以Y = bmth(unit, quan)總是None
  3. 如果您的break不在if中,則while將在第一個循環中斷開。
相關問題