python改错题def app(x、y),学习小甲鱼Python入门(五)习题笔记-函数 第17课课后练习...

函数

def 函数名(参数1,参数2):

求最大公约数

十进制变二进制编写

动动手

def 函数名(参数1,参数2):

求最大公约数

def gcd(x,y):

while y:

t=x%y

x=y

y=t

return x

十进制变二进制编写

def Dec2Bin(dec):

temp=dec#为了print

result=" "

while dec>0:

result+=str(dec%2)

dec//=2

print(temp,'的二进制是:',"ob"+result.lstrip())

str1.lstrip()) 去空格

`

动动手

1、编写两个函数,要求如下

a) 计算打印所有参数的和乘以基数(base=3)的结果

b)如果参数中最后一个参数为(base=5),则设定基数为5,基数不参与求和计算。

def myFun(*param,base=3):

sum1=0

for each in (param):

sum1=sum1+each

print(sum1*base)

>>> myFun(1,2,3,5,4)

45

2、寻找水仙花数字,三位数 个位十位百位数字的立方和 ==这个数字 例如 153=13+53+3**3

list1=[]

for each in range(100,1000) :

a=(each%10)**3

b=((each//10)%10)**3

c=(each//100)**3

if each==a+b+c:

list1.append(each)

print(list1)

>>>

= RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python38-32/00y.py

[153, 370, 371, 407]

** 【小甲鱼】**

for each in range(100,1000):

sum1=0

temp=each

#↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓精彩部分↓↓↓↓↓

while each:

sum1+=(each%10)**3

each=each//10

#↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑精彩部分↑↑↑↑↑↑

if sum1==temp:

print(temp,end=" ")

3、编写一个函数,统计特定字符串,在长字符串中的出现次数 例如 'th’在 On the basis of the analysis of the status quo of China’s marine education and its marine talents training, the author reveals some existing problems.中出现的次数

** 【特别注意】 自定义函数中input的用法 **

def findstr(desstr,substr):#desstr 目标字符串,substr 子字符串

result= desstr.count(substr)

print('子字母串',substr,'在目标字符串',desstr,'中出现了',result,'次')

desstr=input('请输入目标字符串:')

substr=input('请输入子字符串:')

findstr(desstr,substr)

请输入目标字符串:On the basis of the analysis of the status quo of Chinas marine

education its marine talents training, the author reveals some existing problems

请输入子字符串:th

子字母串 th 在目标字符串

On the basis of the analysis of the status quo of Chinas marine education its marine

talents training, the author reveals some existing problems 中出现了 5 次

result= desstr.count(substr),如果用 index的话 ,结果只有3次,不知道为嘛

【小甲鱼】

def findstr(desstr,substr):#desstr 目标字符串,substr 子字符串

sum1=0

if substr in desstr:

for each in range(len(desstr)):

if desstr[each]==substr[0]:

if desstr[each+1]==substr[1]:

sum1+=1

print('目标字符串中包含',sum1,'个',substr)

else:

print('指定字符不在目标字符串里面')

desstr=input('请输入目标字符串:')

substr=input('请输入子字符串:')

findstr(desstr,substr)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值