Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is python condition control and cycle control

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is python conditional control and cycle control". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is python conditional control and cycle control".

Catalogue

Preface

One: select the structure:

Second: cyclic structure

Preface

A Python conditional statement is a block of code that determines execution by the execution result of one or more statements (True or False).

The computer can do a lot of automated tasks because it can make its own condition judgment.

For example, enter the age of the user, print different content according to the age, and in the Python program, use the if statement to achieve:

Age = 20if age > = 18: print ('your age is', age) print (' adult')

According to Python's indentation rules, if the if statement determines that it is True, the two lines of indented print statements are executed, otherwise, nothing is done.

One: select the structure:

1. Height prediction

Every parent is concerned about their child's adult height. According to the analysis of relevant physiological and health knowledge and mathematical statistics, the factors that affect children's adult height are heredity, eating habits, physical exercise and so on. A child's adult height is closely related to the height of his parents and his or her gender.

Let faHeight be his father's height and moHeight his mother's height. The height prediction formula is:

Male adult height = (faHeight + moHeight) * .54

Female adult height = (faHeight * 0.923 + moHeight) / 2

In addition, if you like physical exercise, you can increase your height by 2%, and if you have good hygienic eating habits, you can increase your height by 1.5%.

Parameters such as parents' height and whether they like physical exercise are required to be entered from the keyboard.

Print ("- height Forecast Mini Program -") faHeight=float (input ("Please enter the father's height (cm):") moHeight=float (input ("Please enter the mother's height (cm):") sex=int (input ("Please enter your gender (1: for male, 0: for female):") a=int (input ("whether you like exercise (1: represents yes 2: for no): ") b=int (input (" whether you have a healthy eating habit (1: yes, 2: no): ") if (sex = = 1): # print (" you are a boy! ") If (a = = 1): # print ("you have the habit of physical exercise!") If (b = = 1): # print ("you have a healthy diet!") Height= (faHeight+moHeight) * 0.54-1.02-1.015 print ("you are a boy, in view of your love of exercise and healthy diet, your height is:" + str (height)) else: height= (faHeight+moHeight) * 0.54-1.02 print ("you are a boy, since you love exercise Your height is: "+ str (height)) else: if (b = = 1): # print (" you have a healthy diet! ") Height = (faHeight + moHeight) * 0.54 * 1.015 print ("you are a boy, considering your healthy diet, your height is: + str (height)) else: height = (faHeight + moHeight) * 0.54 print (" you are a boy, in view of your lack of exercise and healthy diet, your height is: "+ str (height)) else: # print (" you are a girl! ") If (a = = 1): # print ("you have the habit of physical exercise!") If (b = = 1): # print ("you have a healthy diet!") Height = (faHeight * 0.923 + moHeight) / 2 * 1.02 * 1.015 print ("you are a girl, in view of your love of exercise and healthy diet, your height is: + str (height)) else: height = (faHeight * 0.923 + moHeight) / 2 * 1.02 print (" you are a girl, since you love exercise Your height is: "+ str (height)) else: if (b = = 1): # print (" you have a healthy diet! ") Height = (faHeight * 0.923 + moHeight) / 2 * 1.015 print ("you are a girl, considering your healthy diet, your height is: + str (height)) else: height = (faHeight * 0.923 + moHeight) / 2 print (" you are a girl, in view of your lack of exercise and hygienic diet, your height is: "+ str (height))

2. Calculate the total amount of sales

The retail price of a book in a book wholesale store is 26.5 yuan per copy. If the customer buys more than 100 copies (including 100 copies) at one time, the price of each book is 10% off. If the customer buys more than 500 copies at one time (including 500 copies), then the price of each book is reduced by 20% and return 1000 yuan to the customer. Please calculate the amount payable for purchasing 8,150,600 copies respectively. The number of books required to be purchased is entered from the console.

Print ("Please enter the number of books you purchased three times:") for i in range (3): num = int (input ("Please enter the total number of books you purchased:") if (num < 100): cost = 26.5 * num print ("you need to pay:" + str (cost) + "Yuan") elif (num < 500): cost = 26.5 * num * 0.9 print ("since you buy more than 100 books 10% discount has been given to you, you need to pay: "+ str (cost) +" yuan ") else: cost = 26.5 * num * 0.8 print (" since you have bought more than 500 books, you have already given a 20% discount, you need to pay: + str (cost) + "yuan")

3. Enter two integers and output them in order from small to large.

# 3. Enter two integers and output the two numbers num1=int (input ("Please enter the first integer:")) num2=int (input ("Please enter the second integer:") if (num10): num=num/2-2 count+=1 print (count) print ("Watermelon in", count, "sold out")

2. Calculate the sum of all integers within 1000 that are not divisible by 7.

# method 1: sum=0for i in range (1p1000): if (I% 7! = 0): sum+=iprint ("the sum of all integers within 1000 that are not divisible by 7 is:", sum) # method 2: i=1sum1=sum2=0while I

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report