11111111111111111
1111吧
全部回复
仅看楼主
level 3
11
2025年10月09日 06点10分 1
level 3
try:
m = int(input("请输入一个数:"))
n = int(input("请输入一个数:"))
p = m/n
except ZeroDivisionError:
print("除数不能为0")
except ValueError:
print("输入错误,请输入整数")
else:
print("计算成功,结果为:")
print(p)
finally:
print("程序执行完毕")
2025年10月13日 06点10分 14
level 3
def divide(a, b):
if b == 0:
raise ValueError("除数不能为零")
return a / b
try:
result = divide(10, 0)
print("结果:", result)
except ValueError as e:
print("发生异常:", str(e))
2025年10月13日 06点10分 15
level 3
# 定义一个Dog类,在类中定义一个变量tooth,
# 并为该变量赋一个初值yellow,在类中定义一个构造方法,
# 方法为Dog对象增加两个实例变量(name和age)并赋初值,
# 再定义一个方法bark,显示Dog叫声。
# 1. 定义一个狗类
class Dog:
tooth = "yellow" # 定义一个固有属性并赋值
def __init__(self, name1, age1): # 创建一个构造方法:用来创建对象的时候进行引用赋值
网页链接 = name1 # 左边是属性,右边是参数
网页链接 = age1
def bark(self, speak): # 定义一个普通方法
print("狗的叫声是", speak)
# 2. 创建狗类的对象
dog1 = Dog("欢欢", 5) # 实际通过调用构造方法来创建对象
print(dog1.age) # 调用对象的属性:对象.属性
print(dog1.name)
print(dog1.tooth)
dog1.bark("汪汪汪") # 调用对象的方法:对象.方法
2025年10月16日 07点10分 17
level 3
class Dog:
eye = "眼睛较大"
body = "身上有黑白相间的花纹"
tail = "尾巴比较短"
num = 0
def __init__(self):
网页链接 += 1
print("\n我是第" + str(Dog.num) + "只狗,我属于狗类!我有以下特征:")
print(Dog.eye)
print(Dog.body)
print(Dog.tail)
list1 = []
for i in range(4):
list1.append(Dog())
print("一共有" + str(Dog.num) + "只狗。")
2025年10月16日 07点10分 18
level 1
# 第一段代码
def add(*args):
total = 0
for val in args:
total += val
return total
print(add())
print(add(1))
print(add(1, 2))
print(add(1, 2, 3))
print(add(1, 3, 5, 7, 9))
# 第二段代码
def roll_dice(n=2):
"""摇色子"""
total = 0
for _ in range(n):
total += randint(1, 6)
return total
def add(a=0, b=0, c=0):
"""三个数相加"""
return a + b + c
print(roll_dice())
print(roll_dice(3))
print(add())
print(add(1))
print(add(1, 2))
print(add(1, 2, 3))
print(add(c=50, a=100, b=200))
# 第三段代码
def fac(num):
"""求阶乘"""
result = 1
for n in range(1, num + 1):
result *= n
return result
m = int(input('m = '))
n = int(input('n = '))
print(fac(m) // fac(n) // fac(m - n))
2025年10月17日 06点10分 19
level 1
# 定义求和函数
def sum_function(a, b):
return a + b
# 输入数据20、30并打印结果
result = sum_function(20, 30)
print("求和结果:", result)
2025年10月17日 06点10分 20
level 1
# 定义判断素数的函数
def is_prime(num):
# 处理特殊情况:小于2的数不是素数
if num < 2:
return False
# 从2到num的平方根遍历,判断是否有因数
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
# 示例调用(可传入任意整数测试)
test_num = 7 # 可替换为其他数字
print(f"{test_num} 是否为素数:", is_prime(test_num))
2025年10月17日 06点10分 21
level 3
# 定义一个Dog类,在类中定义一个变量tooth,
# 并为该变量赋一个初值yellow,在类中定义一个构造方法,
# 方法为Dog对象增加两个实例变量(name和age)并赋初值,
# 再定义一个方法bark,显示Dog叫声。
# 1. 定义一个狗类
class Dog:
tooth = "yellow" # 定义一个固有属性并赋值
def __init__(self, name1, age1): # 创建一个构造方法:用来创建对象的时候进行引用赋值
网页链接 = name1 # 左边是属性,右边是参数
网页链接 = age1
def bark(self, speak): # 定义一个普通方法
print("狗的叫声是", speak)
# 2. 创建狗类的对象
dog1 = Dog("欢欢", 5) # 实际通过调用构造方法来创建对象
print(dog1.age) # 调用对象的属性:对象.属性
print(dog1.name)
print(dog1.tooth)
dog1.bark("汪汪汪") # 调用对象的方法:对象.方法
2025年10月17日 06点10分 22
level 1
# 递归计算1+2+……+n
def qiuhe(n):
if n == 1:
return 1
else:
return n + qiuhe(n-1)
print(qiuhe(100))
print(qiuhe(50))
# 递归求斐波那契数列
def fei(n):
if n == 1:
return 0
elif n == 2:
return 1
else:
return fei(n-1) + fei(n-2)
print(fei(3))
2025年10月17日 06点10分 23
level 1
# (1) 显示"no parameter",匿名函数无参数
a = lambda: "no parameter"
print(a())
# (2) 两个数的差,定义时无默认值,调用时传5,9和3,2
b = lambda x, y: x - y
print(b(5, 9), b(3, 2))
# (3) 求x的y次方,默认值2,3,分三次调用
c = lambda x=2, y=3: x ** y
print(c())
print(c(4))
print(c(3, 4))
# (4) 定义函数test,返回b,并用b(3,2)作为test的参数
def test(b):
return b
print(test(b(3, 2)))
2025年10月17日 06点10分 24
level 1
def max_of_three(a, b, c):
"""求三个数的最大值"""
return max(a, b, c)
def min_of_three(a, b, c):
"""求三个数的最小值"""
return min(a, b, c)
2025年10月17日 06点10分 27
level 5
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:text="存硬盘"
android:textColor="#009688"
android:textSize="18sp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="95dp"
android:text="读硬盘"
android:textColor="#009688"
android:textSize="18sp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="180dp"
android:text="存SD卡"
android:textColor="#009688"
android:textSize="18sp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="265dp"
android:text="读SD卡"
android:textColor="#009688"
android:textSize="18sp" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_marginLeft="10dp"
android:text="存SP"
android:textColor="#009688"
android:textSize="18sp" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_marginLeft="95dp"
android:text="读SP"
android:textColor="#009688"
android:textSize="18sp" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_marginLeft="180dp"
android:text="JSON解析"
android:textColor="#009688"
android:textSize="18sp" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="150dp"
android:text="TextView"
android:textColor="#9C27B0"
android:textSize="20sp" />
</RelativeLayout>
2025年11月14日 03点11分 28
1