python第二次作业

1.打印课堂上图案


2.判断一个数是否是质数(素数)


3、设计一个程序,完成(英雄)商品的购买(界面就是第一天打印的界面)展示商品信息(折扣)->输入商品价格->输入购买数量->提示付款输入付款金额->打印购买小票(扩展)


4、有一些四位数,百位数字都是3,十位数字都是6,并且它们既能被2整除,又能被3整除,求这样的四位数中最大的和最小的两数各是几?

nums = []
for a in range(1000,10000):
	q = a // 1000
	b = a % 1000 // 100
	s = a // 10 % 10
	g = a % 10
	if a % 6 == 0 and b == 3 and s == 6:
		nums.append(a)
 
print(f"最大值:{max(nums)}")
print(f"最小值:{min(nums)}")


5、编程求一个四位自然数ABCD,它乘以A后变成DCBA


6、用户输入两个数a、b。如果a能被b整除或a加b大于1000,则输出a;否则输出b。


7、请输入一个数,判断这个数是偶数还是奇数,如果使用偶数,请判断从1到该数是3的倍数有哪些,如果是奇数,请判断从1到该数是5的倍数有哪些


8、某商店T恤的价格为35元/件(2件9折,3件以上8折),裤子的价格为120 元/条(2条以上9折).小明在该店买了3件T恤和2条裤子,请计算并显示小明应该付多少钱?

a = int(input("请输入买T恤的件数:"))
b = int(input("请输入买裤子的条数:"))
 
if a == 1:
	print("应付35元")
elif a == 2:
	dollar = (35 + 35)* 0.9
	print(f"应付{dollar}元")
elif a >= 3:
	dollar = 35 * a * 0.8
	print(f"应付{dollar}元")
 
if b == 1:
	print("应付120元")
elif b >= 2:
	dollar = 120 * b * 0.9
	print(f"应付{dollar}元")


9、鸡兔同笼,从上面看有35个头,从下面看有94只脚,请问鸡有几只,免有几只?

def solve_chicken_rabbit(heads, legs):
   
    for chickens in range(heads + 1):
        rabbits = heads - chickens
        if 2 * chickens + 4 * rabbits == legs:
            return (chickens, rabbits)
    return (None, None)

def main():
    heads = int(input("请输入头的个数:"))
    legs = int(input("请输入腿的个数:"))
    chickens, rabbits = solve_chicken_rabbit(heads, legs)
    if chickens is not None and rabbits is not None:
        print(f"鸡有 {chickens} 只,兔子有 {rabbits} 只。")
    else:
        print("无法解决问题。")

if __name__ == "__main__":
    main()

10,猜数字游戏(使用random模块完成)
电脑随机一个范围内的数,用户输入数据判断如果数大了,提供”数大了"
成功之后,加上用户是否继续功能

11、猜拳游戏:石头、剪刀、布的游戏

import random

def get_user_choice():
    """获取用户选择"""
    while True:
        user_choice = input("请选择(石头/剪刀/布):").strip().lower()
        if user_choice in ['石头', '剪刀', '布']:
            return user_choice
        else:
            print("请选择石头、剪刀或布。")

def get_computer_choice():
    """随机生成计算机选择"""
    choices = ['石头', '剪刀', '布']
    return random.choice(choices)

def determine_winner(user_choice, computer_choice):
    """确定胜者"""
    if user_choice == computer_choice:
        return "平局"
    elif (user_choice == '石头' and computer_choice == '剪刀') or \
         (user_choice == '剪刀' and computer_choice == '布') or \
         (user_choice == '布' and computer_choice == '石头'):
        return "恭喜,你赢了!"
    else:
        return "很遗憾,你输了。"

def main():
    print("欢迎来到猜拳游戏!")
    while True:
        user_choice = get_user_choice()
        computer_choice = get_computer_choice()
        print(f"你选择了:{user_choice}")
        print(f"计算机选择了:{computer_choice}")
        result = determine_winner(user_choice, computer_choice)
        print(result)
        
        play_again = input("是否继续游戏?(yes/no):").strip().lower()
        if play_again != 'yes':
            print("谢谢游玩,再见!")
            break

if __name__ == "__main__":
    main()

12、用户登录注册案例

class User:
    def __init__(self, username, password):
        self.username = username
        self.password = password

class UserDatabase:
    def __init__(self):
        self.users = {}

    def add_user(self, username, password):
        if username in self.users:
            print("Username already exists. Please choose a different username.")
        else:
            self.users[username] = User(username, password)
            print("User registered successfully.")

    def login(self, username, password):
        if username in self.users and self.users[username].password == password:
            print("Login successful.")
        else:
            print("Invalid username or password.")

def main():
    db = UserDatabase()

    while True:
        print("\n1. Register")
        print("2. Login")
        print("3. Exit")
        choice = input("Enter your choice: ")

        if choice == "1":
            username = input("Enter username: ")
            password = input("Enter password: ")
            db.add_user(username, password)
        elif choice == "2":
            username = input("Enter username: ")
            password = input("Enter password: ")
            db.login(username, password)
        elif choice == "3":
            print("Exiting...")
            break
        else:
            print("Invalid choice. Please try again.")

if __name__ == "__main__":
    main()

13、求50~150之间的质数是那些?

14、打印输出标准水仙花数,输出这些水仙花数

15、验证:任意一个大于9的整数减去它的各位数字之和所得的差,一定能被9整除.

16、一个五位数,若在它的后面写上一个7,得到一个六位数A, 若在它前面写上一个7,得到一个六位数B,B是A的五倍,求此 五位数.

17、有一堆硬币,每次只能拿一个或者两个,求最少多少次可以拿完硬币 [10, 8, 5, 3, 27, 99]

coins = int(input('请输入一堆硬币的个数:'))
if coins % 2 == 0:
    print(f'最少{coins/2:.0f}次可以拿完硬币')
else:
    print(f'最少{coins/2 + 1:.0f}次可以拿完硬币')

18、如果两个素数之差为2,这样的两个素数就叫作"孪生数",找出100以内的所有"孪生数".

print("0到100以内的孪生数对有:")
m = 2
for num in range(3, 100, 2):
    for i in range(2, num // 2 + 1):
        if num % i == 0:
            break
    else:
        if num - m == 2:
            print(f'{m},{num}')
        m = num

19、给定一个列表,求最大值(不能使用系统api),求最小值,求平均值、求和

n = int(input("请您输入您想输入的数字个数:"))
ls = []
for i in range(1, n + 1):
    num = float(input(f"请输入第{i}个数:"))
    ls.append(num)
print(f"您输入的数字有:{ls}")
 
ma = ls[0]
mi = ls[0]
su = 0
for i in ls:
    su = su + i
    if i > ma:
        ma = i
    elif i < mi:
        mi = i
print(f"这些数的最大值为:{ma}")
print(f"这些数的最小值为:{mi}")
print(f"这些数的和为:{su}")
print(f"这些数的平均值为:{su/n}")

20、将list中的重复数据去重,至少使用两种方案

第一种:

ls = [1, 2, 2, 3, 4, 5, 5, 6, 9]
print(f"原列表为:{ls}")
 
ls = list(set(ls))
print(f"方案一去重后的列表为:{ls}")

第二种:

ls = [1, 2, 2, 3, 4, 5, 5, 6, 9]
print(f"原列表为:{ls}")
new_ls = []
for i in ls:
    if i not in new_ls:
        new_ls.append(i)
print(f"方案一去重后的列表为:{new_ls}")

21、如何将0-10随机存入列表中

import random
# 创建一个空列表
random_numbers = []
# 设置要生成的随机数的数量,例如10个
number_of_random_numbers = 10
# 生成随机数并添加到列表中
for _ in range(number_of_random_numbers):
    random_number = random.randint(0, 10)
    random_numbers.append(random_number)
 
print(random_numbers)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值