Simple programming code confession mobile version, simple programming code pictures

Hello everyone, the editor is here to answer the following questions for you, simple programming code confession mobile version, simple programming code pictures, now let us take a look!

1. Determine the size of any three numbers and arrange them from largest to smallest

a=input("请输入第一个数:")
b=input("请输入第二个数:")
c=input("请输入第三个数:")
a=int(a)
b=int(b)
c=int(c)
if a>b>c:
    print(a,b,c)
elif a>c>b:
    d=c;c=b;b=d
    print(a,b,c)
elif b>a>c:
    d=b;b=a;a=d
    print(a,b,c)
elif b>c>a:
    d=b;e=c;b=e;c=a;a=d
    print(a,b,c)
elif c>a>b:
    d=c;e=a;f=b;c=f;b=e;a=d
    print(a,b,c)
elif c>b>a:
    d=c;c=a;a=d
    print(a,b,c)
else:
    print(a,b,c)

or

a=input("请输入第一个数:")
b=input("请输入第二个数:")
c=input("请输入第三个数:")
a=int(a)
b=int(b)
c=int(c)
if a>b>c:
    print(a,b,c)
elif a>c>b:
    print(a,b,c)
elif b>a>c:
    print(a,b,c)
elif b>c>a:
    print(a,b,c)
elif c>a>b:
    print(a,b,c)
elif c>b>a:
    print(a,b,c)
else:
    print(a,b,c)

2. Determine leap year

i=int(input("请输入年份:"))
if ((i%4==0) and (i%100!=0)):
    print("%d年是闰年"%i)
else:
    print("%d年不是闰年"%i)

3. Grades are divided by grade

score=int(input("请输入您的成绩:"))
def func(score):
    if score >100 or score <=0:
        return"wrong score .must between 0 and 100."
    elif score >= 90:
        return"A"
    elif score >= 80:
        return "B"
    elif score >= 70:
        return "C"
    elif score >=60:
        return "D"
    else:
        return "E"
print(func(score))

或者用嵌套选择结构

score=int(input("请输入您的成绩:"))
def func(score):
    degree = "DCBAAE"
    if score >100 or score <=0:
        return"wrong score .must between 0 and 100."
    else:
        index =(score-60)//10
        if index >= 0:
            return degree[index]
        else:
            return[-1]
print(func(score))

      

4. Calculate the value of 1 added to 100

s=0;n=1
while n<=100:
    s=s+n
    n=n+1
print(s)

或者用for-else语句配合使用

s=0
for i in range(1,101):
    s=s+i
else:
    print(s)
更多源码、Python知识手册+薇欣:16521527841 备注CSDN 无套路直接发


5.Print the following graphics

*
**
***
****
*****

i=1
while i<=5:
    j=1
    while j<=i:
        print("*",end="")
        j=j+1
    print("\n")
    i=i+1

6. Calculate the sum of odd numbers and even numbers from 1 to 100
 

n=1;sum_odd = 0;sum_even = 0
while n <= 100:
    if (n%2==0):
        sum_even = sum_even + n
    else:
        sum_odd = sum_odd + n
    n=n+1
print("奇数和为%d,偶数和为%d" %(sum_odd,sum_even))

或者

sum_odd = 0;sum_even = 0
for n in range(1,101):
    if (n%2==0):
        sum_even = sum_even + n
    else:
        sum_odd = sum_odd + n
print("奇数和为%d,偶数和为%d" %(sum_odd,sum_even))

7. If the procedure is as follows

for i in range(6):
    print(i,end="*****")
    i=i-2
    print(i)

输出结果为

0*****-2
1*****-1
2*****0
3*****1
4*****2
5*****3
更多源码、Python知识手册+薇欣:16521527841 备注CSDN 无套路直接发


8.The procedure is as follows

a_list = ["a","b","apple","red"]
for i,v in enumerate(a_list):
    print("列表的第%d个元素为%s" %(i+1,v))

输出结果为

列表的第1个元素为a
列表的第2个元素为b
列表的第3个元素为apple
列表的第4个元素为red

9. Print the multiplication table

for i in range(1,10):
    for j in range(1,i+1):
        print("{0}*{1}={2}" .format(i,j,i*j),end=" ")
    print()


输出结果
1*1=1   
2*1=2   2*2=4   
3*1=3   3*2=6   3*3=9   
4*1=4   4*2=8   4*3=12   4*4=16   
5*1=5   5*2=10   5*3=15   5*4=20   5*5=25   
6*1=6   6*2=12   6*3=18   6*4=24   6*5=30   6*6=36   
7*1=7   7*2=14   7*3=21   7*4=28   7*5=35   7*6=42   7*7=49   
8*1=8   8*2=16   8*3=24   8*4=32   8*5=40   8*6=48   8*7=56   8*8=64   
9*1=9   9*2=18   9*3=27   9*4=36   9*5=45   9*6=54   9*7=63   9*8=72   9*9=81   


10.The procedure is as follows

for i in range(1,101):
    if (i%7==0) and (i%5!=0):
        print(i,end="  ")

输出结果为

7  14  21  28  42  49  56  63  77  84  91  98 

 11.As follows

for i in range(10):
    if (i==5):
        print("found it! i=%d" %i)
else:
    print("not found it...")

输出结果为

found it! i=5
not found it...
更多源码、Python知识手册+薇欣:16521527841 备注CSDN 无套路直接发


12, as follows

for i in range(10):
    if (i==5):
        print("found it! i=%d" %i)
        break
else:
    print("not found it...")


输出结果为

found it! i=5

13, as follows

for i in range(5):
    print("-----")
    print (i)

输出结果
-----
0
-----
1
-----
2
-----
3
-----
4

14.As follows

for i in range(5):
    print("----")
    if (i==3):
        continue
    print(i)

输出结果

----
0
----
1
----
2
----
----
4
更多源码、Python知识手册+薇欣:16521527841 备注CSDN 无套路直接发


15.pass statement

for letter in "runoob":
    if letter == "o":
        pass
        print("执行pass块")
    print("当前字母:%s" %letter)
print("good bye")

输出结果为

当前字母:r
当前字母:u
当前字母:n
执行pass块
当前字母:o
执行pass块
当前字母:o
当前字母:b
good bye

16#####

list = ["ding",123,"true",(1,2,3,"apple"),[1,"小明"],{"name":"ding"}]
print(list[0])
print(list[2])
print(list[1:4])
print(list[:5:2])          #倒着取后两个元素
print(list[-1:-3:-1])
print()

print(list[len(list):])    ###利用切片方式实现列表的增加
list[len(list):] = [222]
print(list)
print()

list.append("good")        #append() 在最后一个位置添加元素
print(list)
print()

list.insert(2,"food")      #insert()按照索引添加,元素插入后,后面的元素索引值会自动加1
print(list)
print()

list.extend("ABC")         #extend() 迭代添加,在最后的位置,迭代每一个元素,依次添加
print(list)
print()

list.pop()                 #pop() 按照索引删除,默认删除最后一个
print(list)
list.pop(1)                #删除索引值为1的元素
print(list)
print()

del list[:3]               #利用切片删除元素
print(list)
print()

list1 = [3,5,7,9]
list1[:3] = [1,2,3]         #利用切片修改列表元素
print(list1)
list1[:3] = []
print(list1)
list1 = list1(range(10))
print(list1)
list1[::2] = [0]*(len(list1)//2)
print(list1)
更多源码、Python知识手册+薇欣:16521527841 备注CSDN 无套路直接发


17.Judges’ scoring

list1 = []
n = int(input("请输入评委人数:"))
while n <= 2:
    print("评委人数需大于2")
    n = int(input("请重新输入评委人数:"))
p = int(input("请输入比赛人数:"))
while p <= 1:
    print("比赛人数需大于1")
    p = int(input("请重新输入比赛人数:"))
def check_score(score):
    if score >= 0 and score <= 100:
        return score
    else:
        print("分数不合法,请重新输入!")
        score = int(input("请重新输入分数:"))
        score = check_score(score)
j = p
list2=[]
while j > 0 :
    name = input("请输入选手序号:")
    list2.append(name)
    i = n
    a = []
    while i > 0:
        score = check_score(int(input("输入评委打分分数:")))
        a.append(score)
        i -= 1
    avg_score = (sum(a) - min(a) - max(a)) / (n-2)
    list1.append([name,avg_score])
    j -= 1
print(list1)

for i in range(p):
    print("选手序号:", list2[i])
    print("平均分:", list1[i][1])

ll=[]
for i in range(p):
    ll.append(list1[i][1])

    ll.sort(reverse=True)
print(ll)

18. Calculate real solutions to quadratic equations of one variable

#coding=utf-8
import math
a=int(input("请输入方程系数a的值"))
b=int(input("请输入方程系数b的值"))
c=int(input("请输入方程系数c的值"))
if ((b*b-4*a*c)>=0)and((a!=0)):
    x1=(-b+math.sqrt(b*b-4*a*c))/(2*a)
    x2=(-b-math.sqrt(b*b-4*a*c))/(2*a)
    print("方程%+d*x*x%+d*x%+d=0的两个实数解为:x1=%+.2f,x2=%+.2f"%(a,b,c,x1,x2))
else:
    print("方程%+d*x*x%+d*x%+d=0无实数解"%(a,b,c))

19.Day of the week

n="1,2,3,4,5,6,7"
data="一 二 三 四 五 六 日"
i=input("请输入阿拉伯数字1-7的任意一个数字:")
if i in n:
    a=data[n.index(i)]
    print("星期{}" .format(a))
更多源码、Python知识手册+薇欣:16521527841 备注CSDN 无套路直接发

Guess you like

Origin blog.csdn.net/mynote/article/details/132912924