Python Programming and algorithm basic course Chapter 4 computer practice

Python version 3.6

The title of this chapter is really much..

  • 4.1
# -*- coding: utf-8 -*-
n=int(input('Type the required number of Yang Hui triangle lines:'))
s1=[1]
for i in range(1,n+1):
    print(str(s1).center(100))
    s1.append(0)
    s2=s1.copy()
    s2.reverse()
    for j in range(0,i):
        s2[j]+=s1[j]
    s1=s2

I have changed the requirements of the topic, and the effect is as follows:

  • 4.2
# -*- coding: utf-8 -*-
import math
a=b=-1
while(a<=0 or b<=0):
    a=float(input('Enter the right side of a right triangle A(>0):'))
    b=float(input('Enter the right side of a right triangle B(>0):'))
c=math.sqrt(a*a+b*b)
l=a+b+c#Perimeter
s=a*b/2
si1=a/c
si2=b/c
d1=round(math.asin(si1)*180/math.pi,0)
d2=round(math.asin(si2)*180/math.pi,0)
print(str.format('The three sides of a right triangle are: A={:0.1f},B={:0.1f},C={:0.1f}',a,b,c))
print(str.format('Circumference of triangle={:0.1f},The measure of area={:0.1f}',l,s))
print(str.format('The two degrees of the acute angle of a triangle are{:0.1f}and{:0.1f}',d1,d2))

The second question is simple

  • 4.3
# -*- coding: utf-8 -*-
import random
a=random.randint(0,100)
b=random.randint(0,100)
c=random.randint(0,100)
print(str.format('Original value: a={},b={},c={}',a,b,c))
print(str.format('(Method 1) ascending value: a={},b={},c={}',min(a,b,c),a+b+c-min(a,b,c)-max(a,b,c),max(a,b,c)))
if a>b:a,b=b,a
if a>c:a,c=c,a
if b>c:b,c=c,b
print(str.format('(Method 2) ascending value: a={},b={},c={}',a,b,c))

I used this sort in the following questions

  • 4.4
# -*- coding: utf-8 -*-
s=-1
while s<=0:
    s=float(input('Please enter the monthly salary of Party members with fixed wage income:'))
if s<=400:d=s*0.005
elif s<=600:d=s*0.01
elif s<=800:d=s*0.015
elif s<=1500:d=s*0.02
elif s>1500:d=s*0.03
print(str.format('Monthly wages={},Pay Party membership fees={}',s,d))

Curious about the standard year, so cheap

  • 4.5
# -*- coding: utf-8 -*-
x=float(input('Please enter the operands x: '))
y=float(input('Please enter the operands y: '))
z=input('Please enter an operator:')
if z=='/' and y==0:
    print('Denominator=0,Zero divide exception')
elif z=='%' and y==0:
    print('Denominator=0,Zero divide exception')
else:
    if z=='+':r=x+y
    if z=='-':r=x-y
    if z=='*':r=x*y
    if z=='/':r=x/y
    if z=='%':r=x%y
    print(str.format('{}{}{}={}',x,z,y,r))

I don't think it's necessary for Python to make input judgment, so I didn't

  • 4.6
# -*- coding: utf-8 -*-
a=float(input('Please enter the edge of the triangle a: '))
b=float(input('Please enter the edge of the triangle b: '))
c=float(input('Please enter the edge of the triangle c: '))
a,b,c=min(a,b,c),a+b+c-min(a,b,c)-max(a,b,c),max(a,b,c)
if a<=0 or b<=0 or c<=0 or a+b<=c or a+c<=b or b+c<=a:print('Can't form triangle!')
elif a==b==c:print('The triangle is equilateral!') 
elif ((a==b!=c)or(a==c!=b)or(c==b!=a))and(a*a+b*b==c*c):
    print('This triangle is isosceles right triangle!')
elif (a==b!=c)or(a==c!=b)or(c==b!=a):print('This triangle is isosceles triangle!')
elif a*a+b*b==c*c:print('The triangle is a right triangle!')
else:print('This triangle is an ordinary triangle!')

If I don't make input judgment, I have to rely on the user not to mess up. I added an isosceles right angle to this question, although... I'm sure no one can input it. Try it?

  • 4.7
# -*- coding: utf-8 -*-
h=-1
f=1
while h<0:
    h=int(input('Please enter the total number of headers:'))
while f%2==1:
    f=int(input('Please enter the total number of feet (must be even):'))
r=f/2-h
if r>h or r<0:
    print('No solution!')
else:  
    print(str.format('Method 1: Chicken:{}Only rabbit:{}only',int(h-r),int(r)))
for r in range(0,h+1):
    if r*2+(h-r)*4==f:
        print(str.format('Method 2: Chicken:{}Only rabbit:{}only',r,h-r))
        break
    if r==h:print('No solution!') 

I choose leather.

  • 4.8
# -*- coding: utf-8 -*-
def fac(a,b=1):#Defining factorial functions
    return b if a==1 or a==0 else fac(a-1,a*b)
r=0
i=0
x=float(input('Please input X:'))
while abs((x**i)/fac(i))>=10**-6:
    r+=(x**i)/fac(i)
    i+=1
print('Pow(e,x)=',r)

Learned the default parameters of functions

  • 4.9
# -*- coding: utf-8 -*-
a=-1
while(a<0):
    a=float(input('Input non negative real number a: '))
r=a/2+0.5
if a==0:print('0 The arithmetic square root of=0')  
else:
    while r-(r+a/r)/2>=10**-6:
        r=(r+a/r)/2
    print(a,'The arithmetic square root of=',r)

Adjust the accuracy a little higher. It's basically the result

The next result, I adjust the precision to the power of 10 to the power of 20

  • 4.10
# -*- coding: utf-8 -*-
i=3
print('0~1000 In addition, 3 is used to divide the remaining 2, 5 is used to divide the remaining 3, and 7 is used to divide the remaining 2')
while 7*i+2<=1000:
    if(7*i+2)%5==3 and (7*i+2)%3==2:
        print(7*i+2,end=' ')
    i+=1

For this problem, I don't add up one by one, but add up seven, which is faster

  • 4.11
# -*- coding: utf-8 -*-
r=0
h=100
for i in range(1,20):
    print('The ball is in the first place.',i,'When landing for the second time, passing',r,'rice')
    r+=h
    h/=2
    print(str.format('The first{}Secondary rebound{}rice',i,h))

The result in this problem book is very mysterious. I took out the 100 in the first paragraph and it was almost the same, but...

The 10th time in the book is 199.80 meters, rebounding 0.20 meters, but the result is the 11th and 9th time

  • 4.12
# -*- coding: utf-8 -*-
r=-0.5
for i in range(8,0,-1):
    print()

Simple, no evaluation

  • 4.13
# -*- coding: utf-8 -*-
import random
n=random.randint(1,10)
t=1
r=0
for i in range(1,n+1):
   r+=t
   t=10*t+1
print(str.format('n={} Sn={}',n,r))

It's also simple.

Keywords: Python

Added by richie1975 on Fri, 03 Apr 2020 11:44:11 +0300