Python:每日一题 125
本帖最后由 冬雪雪冬 于 2017-11-26 20:08 编辑先我们的玩法做了一下改变:
1. 楼主不再提供答案。
2. 请大家先独立思考”,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。
题目:我们知道python的运算中,两个整数相除结果是浮点数,建立一个函数,计算两个数的除法,如果能整除返回整数,负责返回浮点数。
def myDiv(m, n):
myDiv(3, 2)
# 1.5
myDiv(4, 2)
# 2
>>> def div(n,m):
return n/m if n%m else n//m
>>> div(3,2)
1.5
>>> div(4,2)
2
>>> # 125
import math
def myDev(m,n):
result = m/n
if math.modf(result) :
return result
else:
return round(result)
if __name__ == '__main__':
print(myDev(3,2)) #1.5
print(myDev(4,2)) #2
print(myDev(1000001,1000000)) #1.000001
def myDiv(m, n):
if m % n == 0:
return m // n
else:
return m / n def myDiv(m,n):
if (m/n) == int (m/n):
return int(m/n)
else:
return m/n def myDiv(m,n):
if(m%n == 0):
print((int)(m/n))
else:
print(m/n)
def myDiv(a, b):
return a/b if a%b else a//b k = lambda m,n:int(m/n) if m%n == 0 else m/n def myDiv(m,n):
try:
if (m/n).is_integer():
return int(m/n)
else:
return m/n
except ZeroDivisionError as z:
return z def mydiv(m,n):
res = m/n
if m%n == 0:
print('%d' % res)
else:
print(res)
mydiv(4,2)
defmydiv(m, n):
c=m/n
if int(c)==c:
return(int(c))
else:
return(c)
不容易,终于碰到个会做的题...... def mydiv(a,b):
if a % b == 0 :
c = a // b
else :
c = a / b
return c {:10_249:} def myDiv(m,n):
a = m / n
b = int(a)
if a == b:
return b
else:
return a
def myDiv(m, n):
result = m / n
if result - int(result) != 0:
return result
else:
return int(result)
def mydiv(x,y):
if x % y == 0:
return x // y
else:
return x / y
print(mydiv(4,2))
print(mydiv(5,2)) # define a function
def my_div(m, n):
if m % n ==0:
return int(m / n)
else:
return float (m / n ) def myDiv(m, n):
r = m % n
if r == 0:
print(int(m / n))
else:
print(float(m / n))
myDiv(int(input("Number 1: ")), int(input("Number 2: "))) def myDiv(m, n):
if m % n == 0:
return int(m/n)
else:
return m/n def myDiv(x,y):
s=x/y
if s%1==0:
s=int(s)
return s
a=int(input('输入一个数:'))
b=int(input('输入一个数:'))
print(myDiv(a,b))
页:
[1]
2