python学习实验报告(第三周)

一、实验目的

1. 理解条件表达式与True/False的等价关系

2. 熟练运用常见选择结构

3. 熟练运用for循环和while循环

4. 理解带else子句的循环结构执行过程

5. 理解break和continue语句在循环中的作用

二、实验内容和实验结果

1. 编写程序,计算小于1000的所有整数中能够同时被5和7整除的最大整数。

for i in range(999, -1, -1):
    if i % 5 == 0:
        if i % 7 == 0:
            print("小于1000的所有整数中能够同时被5和7整除的最大整数为:", i)
            break

在这里插入图片描述

2. 编写程序,计算1+2+。。。+100的和。

total = 0
for i in range(101):
    total += i
print("1+2+。。。+100 = ", total)

图片描述

3. 编写程序输出100以内的素数。

import math

print("100以内的素数包括:")
for i in range(2, 100):
    limit = int(math.sqrt(i))
    for j in range(2,  limit + 2):
        if j == limit + 1:
            print(i, end=" ")
        if i % j == 0:
            break

图片描述

4. 编写程序,实现抓狐狸游戏。假设墙上有5个洞(编号分别为0、1、2、3、4),其中一个洞里有狐狸,人类玩家输入洞口编号,如果洞里有狐狸就抓到了;如果洞里没有狐狸就第二天再来抓。但在第二天人类玩家来抓之前,狐狸会跳到隔壁的洞里。

import random

choose = int(input("请选择抓狐狸的洞口编号:"))
pos = random.randint(0, 4)
while choose != pos:
    print("未抓到狐狸,狐狸在{}号洞口".format(pos))
    choose = int(input("请重新选择洞口编号:"))
    if pos == 0:
        pos += 1
    elif pos == 4:
        pos -= 1
    else:
        pos += random.sample([1, -1], 1)[0]
print("恭喜你,你在{}号洞口抓到了狐狸".format(pos))

图片描述

5. 小明单位发了100元的购物卡,小明到超市买三类洗化用品,洗发水(15元),香皂(2元),牙刷(5元)。要把100元正好花掉,可有哪些购买结合?

print("可能的购买组合有:")
flag = 0
for i in range(7):
    for j in range(51):
        for k in range(21):
            if i*15 + j*2 + k*5 == 100:
                flag += 1
                print("{:2}瓶洗发水,{:2}盒香皂,{:2}支牙刷".format(i, j, k), end="    ")
                if flag % 4 == 0:
                    print("")

图片描述

6. 求两个数的最大公约数和最小公倍数。(提示:公约数一定小于等于两数中的小的那个数,且能同时被两个数整除;公倍数一定大于等于两数中的大数,且是大数的倍数又能被两数中的小数整除)

def GCD(a, b):
    while a % b != 0:
        temp = b
        b = a % b
        a = temp
    return b
num = input("请输入两个整数(空格分隔):")
a, b = (eval(i) for i in num.split(" "))
gcd = GCD(a, b)
lcm = a * b // gcd
print("{}和{}的最大公约数为{},最小公倍数为{}".format(a, b, gcd, lcm))

图片描述

7. 编写程序,求1!+3!+5!+7!+9!。

total = 0
for i in range(1, 10, 2):
    mul = 1
    for j in range(1, i+1):
        mul *= j
    total += mul
print("1!+3!+5!+7!+9! =", total)

图片描述

8. 编写程序,让用户输入一个整数,如果输入的是正数就输出1,如果输入的是负数就输出-1,否则输出0。

num = int(input("请输入一个整数:"))
if num > 0:
    print(1)
elif num < 0:
    print(-1)
else:
    print(0)

图片描述

9. 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

string = input("请输入一行字符:")
letter = digit = space = other = 0
for char in string:
    if char.isalpha():
        letter += 1
    elif char.isalnum():
        digit += 1
    elif char.isspace():
        space += 1
    else:
        other += 1
print("字符串中有{}个字母,{}个数字,{}个空格,{}个其他字"
      "符".format(letter, digit, space, other))

图片描述

10. 某百货公司为了促销,采用购物打折的办法。1000元以上者,按九五折优惠;2000元以上者,按九折优惠;3000元以上者,按八五折优惠;5000元以上者,按八折优惠。编写程序,输入购物款数,计算并输出优惠价。

price = eval(input("请输入购物款数:"))
if price >= 5000:
    preferencial_price = price * 0.8
elif price >= 3000:
    preferencial_price = price * 0.85
elif price >= 2000:
    preferencial_price = price * 0.9
elif price >= 1000:
    preferencial_price = price * 0.95
else:
    preferencial_price = price
print("最终价格为:", preferencial_price)

图片描述

三、实验小结

本次实验中的大部分内容相对简单,题目类型在其他计算机语言的学习中也较为常见。对于新手学习python编程,这是一些不错的题型,同时有其他语言编程基础的人也能较为方便地区分与之前所学语言的区别。

  • 12
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
实验名称:Python万年历 实验目的:使用Python编写一个可以输出任意年份和月份的日历的程序,并学习Python的基本语法和日期时间模块的使用。 实验步骤: 1. 导入模块 在Python中要使用日期时间模块,需要先导入模块。在程序的开头使用以下代码导入日期时间模块。 ```python import datetime ``` 2. 输入年份和月份 使用`input()`函数让用户输入要查询的年份和月份。 ```python year = int(input("请输入要查询的年份:")) month = int(input("请输入要查询的月份:")) ``` 3. 获取当月第一天的星期几 使用`datetime.date()`函数获取当月第一天的日期,再使用`.weekday()`函数获取星期几。 ```python first_day = datetime.date(year, month, 1) weekday = first_day.weekday() ``` 4. 计算当月天数 使用`datetime.timedelta()`函数计算当月的天数。 ```python if month == 12: days = (datetime.date(year+1, 1, 1) - datetime.timedelta(days=1)).day else: days = (datetime.date(year, month+1, 1) - datetime.timedelta(days=1)).day ``` 5. 输出日历 使用循环输出当月的日历。 ```python print("日 一 二 三 四 五 六") for i in range(weekday): print(" ", end="") for i in range(1, days+1): if i < 10: print("", end=" ") print(i, end=" ") if (i+weekday) % 7 == 0 or i == days: print("") ``` 6. 完整代码 ```python import datetime year = int(input("请输入要查询的年份:")) month = int(input("请输入要查询的月份:")) first_day = datetime.date(year, month, 1) weekday = first_day.weekday() if month == 12: days = (datetime.date(year+1, 1, 1) - datetime.timedelta(days=1)).day else: days = (datetime.date(year, month+1, 1) - datetime.timedelta(days=1)).day print("日 一 二 三 四 五 六") for i in range(weekday): print(" ", end="") for i in range(1, days+1): if i < 10: print("", end=" ") print(i, end=" ") if (i+weekday) % 7 == 0 or i == days: print("") ``` 实验结果: 输入年份和月份,程序会输出该月的完整日历,例如输入2022年3月,输出结果如下: ``` 请输入要查询的年份:2022 请输入要查询的月份:3 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ``` 实验总结: 通过这个实验,我学习Python的基本语法和日期时间模块的使用,并且实现了一个实用的小程序。在编写代码的过程中,我也遇到了一些问题,例如计算当月天数时需要考虑闰年的情况,但是通过查找资料和思考,我最终解决了这些问题。这个实验让我更加深入地了解了Python语言,也为我今后的编程学习打下了基础

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值