我想問用戶他們是不是會員,如果是,給他們購買的座位5%的折扣,如果他們不是會員,沒有折扣。我也想顯示那個折扣價。另外,我將如何將所有最終價格相加并同時顯示?我不知道從哪里開始,感謝任何幫助,謝謝。可能存在一些格式問題,但這是我的代碼:
def main():
print("Your final price for Orchestra seats will be $",get_orchp())
print("Your final price for Center seats will be $",get_centerp())
def get_orchp():
ORCH_SEATS = 75
frontseats = float(input('Enter the number of Orchestra seats you want to purchase : '))
Finalorchp = ORCH_SEATS * frontseats
member = str(input('Are you a member of the local theater group? Enter y or n: '))
if member == 'y':
discount = 0.5
disc = Finalorchp * discount
Findiscorchp = Finalorchp - disc
elif member == 'n':
print('There is no discount for non-members')
return Finalorchp
return findiscorchp
def get_centerp():
CENTER_SEATS = 50
middleseats = float(input('Enter the number of Center Stage seats you want to purchase : '))
Finalcenterp = CENTER_SEATS * middleseats
return Finalcenterp
main()
uj5u.com熱心網友回復:
這就是我將如何解決您的所有問題:
def main():
orchp = get_orchp()
centerp = get_centerp()
print(f"Your final price for Orchestra seats will be ${orchp}")
print(f"Your final price for Center seats will be ${centerp}")
print(f'Your final price for all tickets is {orchp centerp}')
def get_orchp():
ORCH_SEATS = 75
frontseats = float(input('Enter the number of Orchestra seats you want to purchase : '))
Finalorchp = ORCH_SEATS * frontseats
member = str(input('Are you a member of the local theater group? Enter y or n: '))
if member == 'y':
Finalorchp *= .95
return Finalorchp
else:
print('There is no discount for non-members')
return Finalorchp
def get_centerp():
CENTER_SEATS = 50
middleseats = float(input('Enter the number of Center Stage seats you want to purchase : '))
Finalcenterp = CENTER_SEATS * middleseats
return Finalcenterp
main()
請注意這些
- 我更改了對您的函式的呼叫位置并設定了一個變數來接收它們
- 我將最終價格的列印結果更改為 f 字串以接收來自函式的變數
- 在 if 成員陳述句下將 Finalorchp 更改為 variable = variable * .95 的 pythonic 版本
- 將 get_orchp 中的 else 陳述句更改為 else,以防用戶不僅輸入 y 或 n(您可以添加到此以具有容錯能力,如果它不是 y 或 n 然后執行其他操作)
- 添加了另一個帶有 f 字串的最終價格列印,以添加從函式接收 2 個變數的兩個變數。
uj5u.com熱心網友回復:
這段代碼做你想做的事。您可以看到,在代碼中,我從 getorchp() 函式回傳了價格和折扣金額作為元組,并使用它在主函式中列印了實際價格和折扣。如果用戶不是會員,則折扣為零,在主要功能中,我添加了管弦樂隊座位和中心座位價格并列印了最終總價格。此代碼回答了您提出的所有問題
def main():
orc = get_orchp()
print("Your final price for Orchestra seats will be $",orc[0], " and discount is ", orc[1])
cen = get_centerp()
print("Your final price for Center seats will be $",cen)
print("total final price", orc[0] cen)
def get_orchp():
ORCH_SEATS = 75
frontseats = float(input('Enter the number of Orchestra seats you want to purchase : '))
Finalorchp = ORCH_SEATS * frontseats
member = str(input('Are you a member of the local theater group? Enter y or n: '))
if member == 'y':
discount = 0.5
disc = Finalorchp * discount
Findiscorchp = Finalorchp - disc
return (Findiscorchp, disc)
elif member == 'n':
print('There is no discount for non-members')
return (Finalorchp, 0)
def get_centerp():
CENTER_SEATS = 50
middleseats = float(input('Enter the number of Center Stage seats you want to purchase : '))
Finalcenterp = CENTER_SEATS * middleseats
return Finalcenterp
main()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/435756.html
標籤:Python
