In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use judgment sentences in Python. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
1. Interchange between inch and centimeter in imperial units
1 cm = 0.39 inch; 1 inch = 2.54 cm
Do not add branch structure
Convert inches to centimeters
Inches = float (input ("Please enter imperial inches:") cm = inches * 2.54print ('% .2f inches equals% .2f cm'% (inches,cm))
Convert centimeters to inches
Cm = float (input ("Please enter metric units cm:") inches = cm * 0.39print ('% .2f cm equals% .2f inch'% (cm,inches))
Add branch structure
Length = float (input ("Please enter length:") unit = input ("Please enter units:") if unit = = 'in' or unit = =' inch': print (% .2f inch equals% .2f cm'% (length,lenth * 2.54)) elif unit = = 'cm' or unit = =' cm': print (% .2f cm equals% .2f inch'% (length) Length * 0.39) else: print ('the unit you entered is out of line Carry away, the next ~') 2. The score of the percentile system will be converted to the grade system.
Requirements: if the input score is more than 90 points (including 90 points), the output Ascape score is 80 points-90 points (excluding 90 points), the output Bash score is 70 points-80 points (excluding 80 points), the output score is 60 points-70 points (excluding 70 points), and the output score is below 60 points for E.
Logic:
Scores = int (input ("Please enter your score:") if scores > = 90: print ("your rating: a") elif scores > = 80: print ("your rating: B") elif scores > = 70: print ("your rating: C") elif scores > = 60: print ("your rating: d") else: print ("your rating: e")
Optimization:
Scores = int (input ("Please enter your score:") if scores > = 90: grade = 'A'elif scores > = 80: grade =' B'elif scores > = 70: grade = 'C'elif scores > = 60: grade =' D'else: grade = 'E'print ("your rating is% s"% grade) 3. Enter the length of three sides and calculate the perimeter and area if you can form a triangle.
The side length L of a triangle is equal to the sum of three sides.
The area of a triangle can be calculated by Helen's formula.
Helen's formula:
First edition
A = 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:") l = a + b + cprint (the perimeter of the triangle is% .2f'% l) p = (a + b + c) / 2s = (p (p-a) (p-b) (p-c)) * 0. 5print ('the area of the triangle is .2f' s)
The problem with the above calculation method is that when we input the side length of the triangle is 1, 2, 3, it will still have a result; and we know that 1, 2, 3 can not form a trigon.
Optimized version
The problem with the above calculation method is that when we input the side length of the triangle is 1, it will still have a result. And we know that 1pm 2 3 can not form a triangular optimization version a = 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:") if (a + b > c) and (a + c > b) and (a + c > b): l = a + b + c print (the perimeter of the triangle is%. 2f'%l) p = (a + b + c) / 2s = (p * (p-a) * (p-b) * (p-c)) * * 0.5print ('triangle area is% .2f'% s) else: print ('this tm is definitely here to make trouble'):
The reason why a computer can do a lot of flexible tasks is that it can make conditional judgments.
For example, input the age of the customer and print different content according to the age. In the Python program, the if statement is implemented:
Age = 20 if age > = 18: print ('your age is', age) print (' adult')
According to the indentation rules of Python, if the if statement determines that it is True, the indented two print statements are executed, otherwise, nothing is done.
You can also add an else statement to if, meaning that if if determines that it is False, do not execute the contents of if, go ahead and execute else:
Age = 3 if age > = 18: print ('your age is', age) print (' adult') else: print ('your age is', age) print (' teenager')
Be careful not to write less colons:. Of course, the judgment is very rough, and it is entirely possible for elif to make a more detailed judgment:
Age = 3 if age > = 18: print ('adult') elif age > = 6: print (' teenager') else: print ('kid')
Elif is an acronym for else if, and you can have multiple elif, so the complete form of the if statement is:
If: elif: elif: else:
The characteristic of if statement sticking is that it judges from top to bottom. If it is True in a certain judgment, the remaining elif and else will be ignored after executing the corresponding statement of the judgment.
Age = 20 if age > = 6: print ('teenager') elif age > = 18: print (' adult') else: print ('kid') about how to use judgment sentences in Python is shared here. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.