Python - 100天從新手到大師 (感謝作者:駱昊)學(xué)習(xí)筆記
Day01 - 初識Python
Day02 - 語言元素
練習(xí):
1.將華氏溫度轉(zhuǎn)換為攝氏溫度F = 1.8C + 32
f = float(input('請輸入華氏溫度: '))
c = (f - 32) / 1.8
print('%.1f華氏度 = %.1f攝氏度' % (f, c))
2.輸入半徑計算圓的周長和面積
import math
r = (float)(input("輸入圓的半徑:"))
perimeter = 2 * math.pi*r
print(perimeter)
area = math.pi * r * r
print(area)
3.輸入年份 如果是閏年輸出True 否則輸出False
year = input("輸入年份:")
is_leap = (year % 4 == 0 and year % 100 != 0 or year % 400 == 0)
print(is_leap)
報錯:TypeError: not all arguments converted during string formatting

主要是因為運算符%前面和后面的參數(shù)數(shù)據(jù)類型不對應(yīng)
修改后:
year = (int)(input("輸入年份:"))
is_leap = (year % 4 == 0 and year % 100 != 0 or year % 400 == 0)
print(is_leap)