Python函數(shù)
一恭应、定義g到kg的轉(zhuǎn)換函數(shù)饰抒,效果如圖:
def change(a):
print(str(float(a)*0.001) + 'kg')
a = input('請(qǐng)輸入數(shù)字:')
change(a)
def ghuankg(a):
return a/1000
x = input("請(qǐng)輸入數(shù)字:")
print(ghuankg(x))
二智什、輸入姓名页响,隱藏名字,效果如圖:
提示:字符串的索引切片操灿、字符串的replace方法
def change_name(name):
hiding_number = name.replace(name[1:], '*' * (len(name)-1))
print(hiding_number)
a = input('請(qǐng)輸入姓名:')
change_name(a)
def xingming(a):
if len(a)<2:
print(a)
elif len(a)==2:
print(a[len(a)-1]+'*')
elif len(a)>2:
print(a.replace(a[1:(len(a)+1)],'*'*(len(a)-1)))
xingming('哈')
xingming('哈哈')
xingming('1234')