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)