Browse Source

first commit

Pchen. 1 year ago
commit
ce610e250e

+ 9 - 0
Untitled-1.py

@@ -0,0 +1,9 @@
+num = input("请输入10个整数:").split()
+x=0
+for i in num:
+    int(num)
+    if num<=0:
+        continue
+    else:
+        x+=num
+print(x)

+ 5 - 0
X = 658.py

@@ -0,0 +1,5 @@
+X = 658
+a = X//100
+b = X//10%10
+c = X%10
+print(a,b,c)

+ 10 - 0
list情况获取.py

@@ -0,0 +1,10 @@
+def func(lst):
+    minnum = min(lst)
+    indexes = [index for index, value in enumerate(lst) if value == minnum]     #使用enumerate()获取列表中元素的下标(index)和值(value),如果值value为最小值minnum,则返回下标index
+    return (minnum,*indexes)
+
+str_input  =  input().split()
+float_list_input  =  list(map(float,str_input))
+
+#  函数调用形式已给出,请在代码实现部分完成定义及计算代码
+print(func(float_list_input))

+ 14 - 0
二进制数字转二维码.py

@@ -0,0 +1,14 @@
+from PIL import Image
+MAX = 25
+pic = Image.new("RGB",(MAX, MAX))
+str = "1111111000110101101111111100000101110100100100000110111010011100011010111011011101011011011001011101101110100001011010101110110000010101000011010000011111111010101010101111111000000000001101000000000011111011111010111101010101011010011010010110001000110000110100000110100001100111100011100010111000010100011111000111011011110110011000111110011010000010111110011000110011001111011010110100011110100000101110100111101111111011100000000110100111000111001111111011000100101010111100000100110101010001100010111010111010111111101101011101011010011111111011101110101000101000000000110000010110010001010110011111111011110111011100111"
+i = 0
+for y in range (0,MAX):
+    for x in range (0,MAX):
+        if(str[i] == '1'):
+            pic.putpixel([x,y],(0, 0, 0))
+        else:
+            pic.putpixel([x,y],(255,255,255))
+        i = i+1
+pic.show()
+pic.save("flag.png")

+ 17 - 0
企业奖金.py

@@ -0,0 +1,17 @@
+def jiangjin(i):
+    if i <= 100000:
+        t = i * 0.1
+    elif 100000 < i < 200000:
+        t = 10000*0.1 + (i-100000)*0.075
+    elif 200000 <= i <400000:
+        t = 100000*0.1 + 100000*0.075 + (i-200000)*0.05
+    elif 400000 <= i <600000:
+        t = 100000*0.1 + 100000*0.075 + 200000*0.05 + (i-400000)*0.03
+    elif 600000 <= i <1000000:
+        t = 100000*0.1 + 100000*0.075 + 200000*0.05 + 200000*0.03 + (i-6000000)*0.015
+    else:
+        t = 100000*0.1 + 100000*0.075 + 200000*0.05 + 200000*0.03 + 400000*0.015 +(i-1000000)*0.01
+    return t
+
+x = jiangjin(float(input()))
+print(f"奖金总数:{x}")

+ 5 - 0
依次删除最后一个字符.py

@@ -0,0 +1,5 @@
+s="python"
+while s!="":
+    for c in s:
+        print(c,end="")
+    s=s[:-1]

+ 11 - 0
列表数据类型.py

@@ -0,0 +1,11 @@
+items = ["apple", "banana", "cherry", "date"]
+prices = [30,20,15,10]
+prices[2] = 25
+items.remove('cherry')
+prices.remove(15)
+items.append('elderberry')
+prices.append(40)
+print("apple: ", prices[0])
+print("商品\t价格")
+for item, price in zip(items, prices):
+    print(f"{item:}\t{price:}")

+ 10 - 0
创建字典.py

@@ -0,0 +1,10 @@
+d = {}
+d['a'] = 'apple'
+d['b'] = 'banana'
+d['b'] = 'blueberry'
+if 'c' in d:
+    print("字符'c'是d的键")
+else:
+    print("字符'c'不是d的键")
+print("d的长度为:", len(d))
+d.clear()

+ 7 - 0
创建文件加入内容.py

@@ -0,0 +1,7 @@
+try:
+    file = open("example.txt", "w")
+    file.write("Hello, World! This is a test file.")
+except Exception as e:
+    print(f"出现错误: {e}")
+finally:
+    file.close()

+ 14 - 0
判定字符串中既有数字也有字母.py

@@ -0,0 +1,14 @@
+def check(s):   #构造函数
+    zimu = False
+    shuzi = False   #初始化变量zimu和shuzi
+    for x in s:
+        if x.isalpha():   #判断是否有字母
+            zimu = True
+        elif x.isdigit():  #判断是否有数字
+            shuzi = True
+        if shuzi and zimu:   #返回计算结果,只有zimu和shuzi全为True时才返回True
+            return True
+    return False
+ #函数构造完毕     
+ 
+print(check(input()))   #接收输入并调用函数输出结果

+ 8 - 0
句子长度.py

@@ -0,0 +1,8 @@
+def count(sentence):
+    words = sentence.split()
+    total_words = len(words)
+    for word in words:
+        print(f"单词:{word},长度:{len(word)}")
+    print(f"整个句子的总单词数:{total_words}")
+sentence = input("请输入一个句子:")
+count(sentence)

+ 17 - 0
各位数字之和.py

@@ -0,0 +1,17 @@
+# 获取用户输入
+user_input = input("请输入一个自然数:")
+
+# 将输入的字符串转换为整数
+num = int(user_input)
+
+# 初始化各位数字之和
+sum_of_digits = 0
+
+# 计算各位数字之和
+while num > 0:
+    digit = num % 10
+    sum_of_digits += digit
+    num = num // 10
+
+# 输出结果
+print(f"自然数{user_input}的各位数字之和为{sum_of_digits}。")

+ 21 - 0
商品价格.py

@@ -0,0 +1,21 @@
+items = ["apple", "banana", "cherry", "date"]
+prices = [30,20,15,10]
+
+# 更新 banana 的价格
+prices[2] = 25
+
+# cherry 被售出,移除其价格和商品
+items.remove('cherry')
+prices.remove(15)
+
+# 添加 elderberry 和其价格
+items.append('elderberry')
+prices.append(40)
+
+# 打印 apple 的价格
+print("apple: ", prices[0])
+
+# 打印所有商品的名称和对应的价格
+print("商品\t价格")
+for item, price in zip(items, prices):
+    print(f"{item:}\t{price:}")

+ 16 - 0
字典.txt

@@ -0,0 +1,16 @@
+定义空字典d
+向d新增2个键值对元素
+修改第2个元素
+判断字符"c"是否是d的键
+计算d的长度
+清空d
+
+
+习题1: 创建一个字典,包含键 "name"、"age" 和 "city",分别对应你的名字,年龄和居住城市。
+习题2:对于给定的字典 {'A': 1, 'B': 2, 'C': 3},编写一个 Python 代码来输出所有的 key。
+习题3:编写一个程序,删除以下字典的 'age' 键 {'name': 'John', 'age': 26, 'job': 'Engineer'}。
+习题4:写一个函数,将字典的键和值互换,即键变为值,值变为键。例如输入:{'a': 1, 'b': 2, 'c': 3},输出:{1: 'a', 2: 'b', 3: 'c'}。
+习题5:给定一个字典,输出其键值对的数量。
+习题6:检查给定的键是否存在于字典中。例如,对于字典 {'a': 1, 'b': 2, 'c': 3},检查 'd' 是否在字典中。
+习题7:对于字典 {'a': 1, 'b': 2, 'c': 3},编写一个 Python 代码来输出所有的 value。
+习题8:创建一个嵌套字典,比如一个字典里包含另一个字典,然后通过键来访问内部的字典。

+ 7 - 0
字典数据类型.py

@@ -0,0 +1,7 @@
+items_price = {"apple":30, "banana":20, "cherry":15, "date":10}
+items_price["banana"] = 25
+del items_price["cherry"]
+items_price["elderberry"] = 40
+print("apple的价格为:", items_price["apple"])
+for item, price in items_price.items():
+    print(f"{item}:{price}")

+ 4 - 0
数列翻转.py

@@ -0,0 +1,4 @@
+nums = list(input().split())
+output = nums[1:]   #取第二个到最后一个数字
+output.reverse()    #翻转数列
+print(" ".join(output))     

+ 19 - 0
查找质数.py

@@ -0,0 +1,19 @@
+def zs(num):
+    if num < 2:
+        return False
+    for i in range(2, num):
+        if num % i == 0:
+            return False
+    return True
+
+start = int(input("请输入范围的起始值:"))
+end = int(input("请输入范围的结束值:"))
+
+found = False
+for num in range(start, end + 1):
+    if zs(num):
+        print("找到了一个质数:", num)
+        found = True
+
+if not found:
+    print("在{}~{}内没有找到质数".format(start,end))

+ 18 - 0
温度转换-构建函数.py

@@ -0,0 +1,18 @@
+def convert_to_celsius(temp_str):
+    C = (eval(temp_str[0:-1]) - 32)/1.8
+    return C
+
+def convert_to_fahrenheit(temp_str):
+    F = 1.8*eval(temp_str[0:-1]) + 32
+    return F
+
+TempStr = input("请输入带有符号的温度值: ")
+
+if TempStr[-1] in ['F', 'f']:
+    C = convert_to_celsius(TempStr)
+    print("转换后的温度是{:.2f}C".format(C))
+elif TempStr[-1] in ['C', 'c']:
+    F = convert_to_fahrenheit(TempStr)
+    print("转换后的温度是{:.2f}F".format(F))
+else:
+    print("输入格式错误")

+ 12 - 0
温度转换.py

@@ -0,0 +1,12 @@
+#TempConvert.py
+TempStr = input("请输入带有符号的温度值: ")
+
+
+if TempStr[-1] in ['F', 'f']:
+    C = (eval(TempStr[0:-1]) - 32)/1.8
+    print("转换后的温度是{:.2f}C".format(C))
+elif TempStr[-1] in ['C', 'c']:
+    F = 1.8*eval(TempStr[0:-1]) + 32
+    print("你输入的温度为:{}".format(TempStr),"转换后的温度是{:.2f}F".format(F))
+else:
+    print("输入格式错误")

+ 7 - 0
生成绝对值.py

@@ -0,0 +1,7 @@
+x = float(input())
+if x>=0:
+    y=x
+else:
+    y=-x
+print(y)
+

+ 2 - 0
生日.py

@@ -0,0 +1,2 @@
+year=input("请输入年龄:")
+print("Happy {}th Birthday!".format(year))

+ 11 - 0
百钱百鸡.py

@@ -0,0 +1,11 @@
+for x in range(0,21):
+        for y in range(0,34):
+                z = 100-x-y
+                if 5*x+3*y+1*z/3==100:
+                    x1=str(x)
+                    y1=str(y)
+                    z1=str(z)
+                    output1=x1+"只,"
+                    output2=y1+"只,"
+                    output3=z1+"只"
+                    print("公鸡:",output1,"母鸡:",output2,"小鸡:",output3)

+ 8 - 0
知两边求三边.py

@@ -0,0 +1,8 @@
+import math
+a,b,theta = input().split()
+a = float(a)
+b = float(b)
+theta = float(theta)
+theta_radians = math.radians(theta)
+c = math.sqrt(a**2 + b**2 - 2*a*b*math.cos(theta_radians))
+print("c = {:.4f}".format(c))

+ 9 - 0
计算65535最大素数.py

@@ -0,0 +1,9 @@
+def sushu(num):
+    for i in range(2,int(num**0.5)):    #取平方根并取整,优化算法
+        if num % i == 0:
+            return False
+    return True
+for num in range(1,65535):
+    if sushu(num):
+        x = num
+print(x)

+ 11 - 0
计算n个a相减.PY

@@ -0,0 +1,11 @@
+def nb(a,n):
+    n = int(n)
+    x = int(a * n)
+    while n > 1:
+        m = int(a * (n-1))
+        x = x-m
+        n = n-1
+    return x
+
+a,n = input().split()
+print(nb(a,n))

+ 7 - 0
进制转换.py

@@ -0,0 +1,7 @@
+num = int(input("请输入一个自然数:"))
+binary = bin(num)
+octal = oct(num)
+hexadecimal = hex(num)
+print("自然数{}的二进制表示为:{}。".format(num, binary))
+print("自然数{}的八进制表示为:{}。".format(num, octal))
+print("自然数{}的十六进制表示为:{}。".format(num, hexadecimal))