Python(练习七)

这篇文章包含多个Python代码示例,涉及循环结构如while和for,以及数学运算。第一部分是找出最大值及其出现次数,第二部分计算随机点落在特定区域的概率,第三部分涉及十进制到二、八、十六进制的转换,第四部分求数字的总和,第五部分进行温度单位转换,第六部分识别闰年,第七部分检查并计算三角形面积,第八部分寻找素数和回文数,最后一部分再次展示进制转换。
摘要由CSDN通过智能技术生成

一、

 

max = 0
count = 0
while True:
    num = int(input("Enter a number (0: for end of input): "))
    if num == 0:
        break
    if num > max:
        max = num
        count = 1
    elif num == max:
        count += 1
print("The largest number is  ",max)
print("The occurrence count of the largest number is  ",count)

二、

# 方法一:
import random
sum = 0
for i in range (0, 1000001):
    x = random.random() * random.choice([-1,1])
    y = random.random() * random.choice([-1,1])
    if x<= 0 or ( x >= 0 and y >= 0 and (y / (1 - x) >= 1)):
        sum += 1
a = sum / 1000000
print("1000000个随机点落在奇数区域的概率是%.12f"%a)

# 方法二:
import random
total = 1000000
count = 0
for i in range(0,total):
    x = random.random() * 2 - 1  # random.random()  产生[0,1)之间任意一个小数
    y = random.random() * 2 - 1  # random.random()  产生[0,1)之间任意一个小数
    if -1 <= x <= 0 and -1 <= y <= 1:
        count += 1
    elif 0 <= x <= 1 and 0 <= y <= 1 and y / (1-x) <= 1:
        count += 1
print("1000000个随机点落在奇数区域的概率是%.12f"%(count / total))

三、

 

# 方法一:
# 分别为转二进制、八进制、十六进制
# 转换的结果为字符串
# >>> bin(18)
# '0b10010'
# >>> oct(18)
# '0o22'
# >>> hex(18)
# '0x12'
Dec = int(input("请输入一个十进制数:"))
print(hex(Dec))

# 方法二:
a = int(input("请输入一个十进制整数:"))
print("{}对应二进制为{:b},八进制为{:o},"
      "十六进制为{:x}".format(a,a,a,a))
# 方法三:
number = int(input("Enter a number:"))
hexStr = ""
while number != 0:
    a = number % 16
    if a < 10:
        hexStr = str(a) + hexStr
    else:
        hexStr = chr(87 + a) + hexStr
    number //= 16
print(hexStr)

四、

def sumDigits(n):
    num = 0
    for i in range(len(str(n))):
        num += n % 10
        n = n // 10
    return num
print(sumDigits(234))

五、

 

while True:
    tem = input("请输入带符号的温度:")
    if tem[-1] == "C" or tem[-1] == "c":
        a = float(tem[0:-1])
        b = a * 1.8 + 32
        c = format(b, ".2f")
        result = str(c)
        print("转换后的温度是:{}F".format(result))
    elif tem[-1] == "F" or tem[-1] == "f":
        a = float(tem[0:-1])
        b = (a - 32) / 1.8
        c = format(b, ".2f")
        result = str(c)
        print("转换后的温度是:{}C".format(result))
    else:
        print("格式输入错误")

 六、

count = 0
for y in range(2001, 2100 + 1):
    if y % 400 == 0 or y % 4 == 0 and y % 100 != 0:
        print(y, end=" ")
        count += 1
        if count % 10 == 0:
            print()

七、

def isValid(side1, side2, side3):
    return side1 + side2 > side3 and side2 + side3 > side1 and side1 + side3 > side2
 
 
def area(side1, side2, side3):
    s = (side1 + side2 + side3) / 2
    return (s * (s - side1) * (s - side2) * (s - side3)) ** 0.5
 
 
s1, s2, s3 = eval(input("请输入三角形三边长:"))
if isValid(s1, s2, s3):
    print("area is %.2f" % area(s1, s2, s3))
else:
    print("Invalid")

八、

from math import*
def isprime(n):
    for j in range(2,int(sqrt(n))+1):
        if n%j==0:
            return 0
    return 1

def ishui(n):
    n1=n[::-1]
    
    if n1==n:
        return 1
    return 0
    
x=int(input())
i=0
n=2
while(i<x):
    
    if(isprime(n) and ishui(str(n))):
        print(n,end=' ')
        i+=1
    n+=1

九、

# 方法一:
# 分别为转二进制、八进制、十六进制
# 转换的结果为字符串
# >>> bin(18)
# '0b10010'
# >>> oct(18)
# '0o22'
# >>> hex(18)
# '0x12'
Dec = int(input("请输入一个十进制数:"))
print(bin(Dec))
#方法二:
number = int(input("Enter a number:"))
"""
 数 / 几进制 = 商 ~ 余数   注意:当商为0时停止,倒着取余数得到二进制数
12 / 2 = 6 ~ 0
6 / 2 = 3 ~ 0
3 / 2 = 1 ~ 1
1 / 2 = 0 ~ 1
1100
"""
binStr = ""
while number != 0:
    a = number % 2
    binStr = str(a) + binStr
    number //= 2
print(binStr)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值