python3中用format怎么把變量(浮點(diǎn)數(shù))轉(zhuǎn)成整數(shù)打印出來
問題描述
#!/usr/bin/env python3# -*- coding: utf-8 -*-’N個(gè)數(shù)字的平均值’N = 3sum = 0count = 0while count < N: num = float(input(’number:’)) sum = num + sum count += 1average = sum / Nprint(type(sum))print('N: %s, sum: %d ,average: %.2f' % (N, sum, average))print('N:{}, sum:{} ,average:{:.2f}'.format(N, sum, average))# print('N:{}, sum:{:d} ,average:{:.2f}'.format(N, sum, average))
最后注釋掉的那一行報(bào)錯(cuò),ValueError: Unknown format code ’b’ for object of type ’float’我想把sum,例如60.0這樣的浮點(diǎn)數(shù),整數(shù)輸出
問題解答
回答1:試下這個(gè)
print('N:{}, sum:{:.0f} ,average:{:.2f}'.format(N, sum, average))
相關(guān)文章:
1. MySQL客戶端吃掉了SQL注解?2. php自學(xué)從哪里開始?3. mysql - AttributeError: ’module’ object has no attribute ’MatchType’4. 數(shù)據(jù)庫 - MySQL 單表500W+數(shù)據(jù),查詢超時(shí),如何優(yōu)化呢?5. 求大神幫我看看是哪里寫錯(cuò)了 感謝細(xì)心解答6. python - Django分頁和查詢參數(shù)的問題7. javascript - 圖片能在網(wǎng)站顯示,但控制臺(tái)仍舊報(bào)錯(cuò)403 (Forbidden)8. javascript - 百度echarts series數(shù)據(jù)更新問題9. phpstady在win10上運(yùn)行10. python小白的基礎(chǔ)問題 關(guān)于while循環(huán)的嵌套
