第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python打印出浮點數或整數

Python打印出浮點數或整數

慕慕森 2021-03-17 13:13:19
如果結果為小數,如何打印浮點數;如果結果沒有小數,如何打印整數?c = input("Enter the total cost of purchase: ")bank = raw_input("Enter the bank of your credit card (DBS, OCBC, etc.): ")dbs1 = ((c/float(100))*10)dbs2 = c-dbs1ocbc1 = ((c/float(100))*15)ocbc2 = c-ocbc1if (c > 200):    if (bank == 'DBS'):        print('Please pay $'+str(dbs2))    elif (bank == 'OCBC'):        print('Please pay $'+str(ocbc2))    else:        print('Please pay $'+str(c))else:    print('Please pay $'+str(c))exit = raw_input("Enter to exit")示例結果Enter the total cost of purchase: 250Enter the bank of your credit card (DBS, OCBC, etc.): OCBCPlease pay $212.5Enter the total cost of purchase: 250Enter the bank of your credit card (DBS, OCBC, etc.): DBSPlease pay $225.0
查看完整描述

3 回答

?
30秒到達戰(zhàn)場

TA貢獻1828條經驗 獲得超6個贊

您可以嘗試使用簡單的Python字符串格式化方法:


if int(c) == float(c):

    decimals = 0

else:

    decimals = 2 # Assumes 2 decimal places for money


print('Please pay: ${0:.{1}f}'.format(c, decimals))

如果滿足以下條件,則將為您提供以下輸出c == 1.00:


Please pay: $1

或此輸出,如果c == 20.56:


Please pay: $20.56


查看完整回答
反對 回復 2021-03-24
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

def nice_print(i):

    print '%.2f' % i if i - int(i) != 0 else '%d' % i


nice_print(44)

44


nice_print(44.345)

44.34

在您的代碼中:


def nice_number(i):

    return '%.2f' % i if i - int(i) != 0 else '%d' % i


c = input("Enter the total cost of purchase: ")

bank = raw_input("Enter the bank of your credit card (DBS, OCBC, etc.): ")

dbs1 = ((c/float(100))*10)

dbs2 = c-dbs1

ocbc1 = ((c/float(100))*15)

ocbc2 = c-ocbc1



if (c > 200):

    if (bank == 'DBS'):

        print('Please pay $'+nice_number(dbs2))

    elif (bank == 'OCBC'):

        print('Please pay $'+nice_number(ocbc2))

    else:

        print('Please pay $'+nice_number(c))

else:

    print('Please pay $'+nice_number(c))


查看完整回答
反對 回復 2021-03-24
  • 3 回答
  • 0 關注
  • 303 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號