In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you the "Python program flow control example analysis", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "Python program flow control example analysis" this article.
1. Write a program to calculate 1, 3, 5, 7... Sum of + 99.
Sum=0for i in range (1m 100jue 2): sum+=iprint (sum)
two。 Write a program to calculate 2, 4, 6, 8... The sum of + 100.
Sum=0for i in range (2meme 101): sum+=iprint (sum)
3. Write a program that uses different implementation methods to output all the leap years of 2000-3000, as shown in the following figure.
Num=0for i in range (2000 and 3001): if (I% 4 percent 0 and i0 percent 0) or I% 400 percent percent 0: num+=1 if num%8==0: print (iminute endurance'\ n') else: print (iminute endurance'\ t') else: i=i+1
4. Write a program to calculate Sn=1-3, 5-7, 9-11 +. N
N=int (input ('Please enter the value of n:') # # method 1: Sn= 0x =-1for i in range: if I% 2quotation 0: # is an even number of Xerox else: Sn+=i*xprint ('Sn=',Sn) # method 2: sum1=0sum2=0for j in range (1Magnum 1): sum1+=jfor k in range: sum2+=kSn=sum1-sum2print (' Sn=',Sn)
5. Write a program to calculate Sn=1+1/2+1/3+. 1/n
N=int (input ('Please enter the value of n:') x=1sum=0for i in range (1 Sn= nasty 1): xresume 1 sum+=xprint ('format {: 2.2f}' .format (format))
6. Write a program and print the 99 multiplication table. (as long as you implement one of the four ways)
It is required to output various display effects of the 99 multiplication table (upper triangle, lower triangle, rectangular block, etc.)
For i in range (1Power10): for j in range (1mem11phili): print ("{0} * {1} = {2}" .format ((10ripi), j, (10ripi) * j), end='\ t') print (end='\ n') for i in range (1dome 10): for j in range (1dome ionome1): print ("{0} * {1} = {2}" .format (iMagejPerfomj) End='\ t') print (end='\ n')
7. Write a program to input the three sides of a triangle, first determine whether it can form a triangle, and if so, further calculate the perimeter and area of the triangle, otherwise report the error "can not form a triangle!" .
Its running effect is shown in the following figure (the results all retain decimal places):
Import matha=float (input ('please enter the side length of the triangle') b=float (input ('please enter the side length of the triangle) c=float (input (' please enter the side length of the triangle)) print ('the three sides of the triangle are: a = {0}, b = {1}, c = {2}' .format (aCodeb) C) l=a+b+ch=l/2area=math.sqrt (h* (Hmura) * (HMub) * (hmerc)) if a > 0 and b > 0 and c > 0 and aquib > c and aquib > b and baccc > a: print ('perimeter of a triangle = {0}' .format (l)) print ('area of a triangle = {0}' .format (area)) else: print ('cannot form a triangle!')
8. Write a program, enter x, and calculate the value of the piecewise function y according to the following formula. Please use the single-branch sentence double-branch structure and conditional operation statements and other methods to achieve.
Import mathx = float (input ('please enter Xinput)) if x > = 0: y = (x * x-3 * x) / (x + 1) + 2 * math.pi + math.sin (x) if x
< 0: y = math.log(-5 * x) + 6 * math.sqrt((-x + math.e ** 4)) - (x + 1) ** 3print('方法一:x={0}, y={1}'.format(x, y)) # 一句单分支会导致错误print('方法二:x={0}, y={1}'.format(x, y))if x >= 0: y = (x * x-3 * x) / (x + 1) + 2 * math.pi + math.sin (x) else: y = math.log (- 5 * x) + 6 * math.sqrt ((- x + math.e * * 4))-(x + 1) * * 3print ('method 3: X = {0}, y = {1}' .format (x Y)) y = (x * x-3 * x) / (x + 1) + 2 * math.pi + math.sin (x) if (x > = 0) else\ math.log (- 5 * x) + 6 * math.sqrt ((- x + math.e * * 4))-(x + 1) * * 3print ('method 4: X = {0}, y = {1}' .format (x, y))
9. Write a program and input three coefficients a, b and c of the unary quadratic equation to find the solution of the ax ²+ bx+c=0 equation. The result is shown in the following figure
Import matha=float (input ('Please enter the coefficient aPlease') b=float (input ('Please enter the coefficient') c=float (input ('Please enter the coefficient')) d=b*b-4*a*c # Discriminant if axiom 0 and coefficient 0: print ('this equation has no solution!') Elif axioms 0 and bounds 0: print ('the solution of this equation is:', end='') x=-c/b print (x) elif dystrophy 0: # discriminant equals 0 print ('this equation has two equal real roots:', end='') x = (- b) / 2 equal a print (x) elif d > 0: # discriminant is greater than 0 print ('this equation has two unequal real roots:' End='') x1 = ((- b) + math.sqrt (d)) / 2yoga x2 = ((- b)-math.sqrt (d)) / 2fanta print (x1Magi 'and', x2) elif d
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.