python作业4

(最大数的出现)编写程序读取整数,找出它们中的最大值,然后计算它的出现次数

mnx = 0
count = 0
while True:
    num = int(input("请输入一个数字 (以数字0结束): "))
    if num == 0:
        break
    if num > mnx:
        mnx = num
        count = 1
    elif num == mnx:
        count += 1
print("最大的数字是:", mnx)
print("最大数字出现的次数是: ", count)

 (十进制到二进制)编写程序提示用户输人一个十进制整数,然后显示它对应的二进制数

Dec = int(input("请输入一个十进制数:"))
print(bin(Dec))

 (十进制到十六进制)编写程序提示用户输人一个十进制数,然后显示它对应的十六进制数

Dec = int(input("请输入一个十进制数:"))
print(hex(Dec))

 (蒙特卡罗模拟)一个正方形被分为四个更小的区域,如图a所示。如果你投掷一个飞镖到这个正方形一百万次,这个飞镖落在一个奇数区域里的概率是多少?编写程序模拟这个过程然后显式结果。(提示:将这个正方形的中心放在坐标系统的中心位置,如图b 所示。在正方形中随机产生一个点,然后统计这个点落入奇数区域的次数。

import random
sm = 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)):
        sm += 1
a = sm / 1000000
print("1000000个随机点落在奇数区域的概率是%.12f" % a)

 (显示闰年)编写程序显示21世纪(从 2001 年到 2100年)里所有的闰年,每行显示 10个闰年,这些年被一个空格隔开。

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

 

 十进制数d转换为十六进制所满足的条件

 (求一个整数各个数字的和)编写一个函数,计算一个整数各个数字的和。使用下面的函数头:

number = int(input("请输入一个数字:"))
 
 
def sumDigits(n):
    sm = 0
    while n != 0:
        sm += n % 10
        n = n // 10

 (反向显示一个整数)编写下面的函数,反向显示一个整数

number = int(input("请输入一个数字:"))
 
 
def reverse(num):
    rum = 0
    while num != 0:
        rum = rum * 10 + num % 10
        num //= 10
    return rum
 
 
print(reverse(number))

(数列求和)编写一个函数计算下面的数列

 

 (MyTriangle 模块)创建一个名叫 MyTriangle 的模块,它包含下面两个函数

# Returns true if the sum of any two sides is

# greater than the third side

def isValid(side1,side2,side3):

# Returns the area of the triangle.

def area(side1,side2,side3):

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")

做一个用户管理系统:

import sys
import hashlib

# 用来存储所有的用户信息
users = []
slat = "lx"


def password_by_md5(password):
    md5 = hashlib.md5(password.encode("utf-8"))
    # 盐值
    md5.update(slat.encode("utf-8"))
    return md5.hexdigest()


def main():
    print("~*" * 20)
    print("")
    print("\t\t 1、用户注册\n")
    print("\t\t 2、用户登录\n")
    print("\t\t 3、退出系统\n")
    print("~*" * 20)

    choice = input("请输入您要操作的选项:")
    return choice


def exists_user(username):
    for i in users:
        if i.get("username") == username:
            return True
    return False


def is_login(username, password):
    for i in users:
        if i.get("username") == username and i.get("password") == password:
            print("登录成功")
            return True
    return False


def register():
    username = input("请输入您的用户名称:")
    password = input("请输入您的用户密码")

    # 校验数据
    if username is None or username.strip() == "":
        print("对不起,用户名不能为空")
        return

    if password is None or password.strip() == "" or len(password) < 3:
        print("对不起,密码长度不能少于3位")
        return

    # 判断该用户是否存在
    if exists_user(username):
        print("对不起,该用户已经存在,请重新输入!!")
        return

    # 组建成一个字典对象
    user = {"username": username, "password": password_by_md5(password)}
    users.append(user)
    print(users)


def login():
    username = input("请输入您的用户名称:")
    password = input("请输入您的用户密码")

    # 加密密码
    password = password_by_md5(password)
    if is_login(username, password):
        print("恭喜你,登录成功")
    else:
        print("对不起,登录失败,请重新登录")


while True:
    choice = main()
    if choice == "1":
        register()

    elif choice == "2":
        login()
    else:
        print("程序正常退出")
        sys.exit(0)

 某个人进入如下一个棋盘中,要求从左上角开始走,最后从右下角出来(要求只能前进,不能后退),问题:共有多少种走法?
0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0
 

x = []
 
for i in range(0, 5):
    x.append([])
    for j in range(0, 8):
        x[i].append(0)
        if i == 0 and j != 0:
            x[i][j] = x[i][j - 1] + 1
        elif i != 0 and j == 0:
            x[i][j] = x[i - 1][j] + 1
        elif i != 0 and j != 0:
            x[i][j] = x[i - 1][j] + x[i][j - 1]
print(f"{x[len(x) - 1][len(x[0]) - 1]}")

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值