python作业二
# 二进制转化为十进制
num = input("num:")
def binaryToDecimal(binaryString):
he = 0
length = len(binaryString)
for i in range(length):
he += int(binaryString[i]) * 2 ** (length - i - 1)
return he
print(binaryToDecimal(num))
代码运行如下:
import math
side1 = int(input("边1:"))
side2 = int(input("边2:"))
side3 = int(input("边3:"))
def isValid(side1, side2, side3):
if side1 + side2 > side3 and side2 + side3 > side1 and side1 + side3 > side2:
return True
else:
print("这不能构成三角形!")
return False
def area(side1, side2, side3):
if isValid(side1, side2, side3):
s = (side1 + side2 + side3) / 2
area = math.sqrt(s * (s - side1) * (s - side2) * (s - side3))
print(area)
area(side1, side2, side3)
代码运行如下:
def isPrime(num):
if num == 2 or num == 3 or num == 5:
return True
elif num == 0 or num == 1:
return False
if num % 6 == 5 or num % 6 == 1:
for i in range(5, int(num ** 0.5) + 1, 6):
if num % i == 0 or num % (i + 2) == 0:
return False
else:
return True
else:
return False
prime_ls = []
for n in range(1000):
if isPrime(n):
prime_ls.append(n)
for j in range(len(prime_ls) - 1):
if prime_ls[j + 1] - prime_ls[j] == 2:
print(prime_ls[j], prime_ls[j + 1])
代码运行如下:
原文地址:https://blog.csdn.net/weixin_45926941/article/details/140320603
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!