Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the structures of python loops

Shulou Source: shulou.com Published: 2022-06-02 16:49:31 09月15日 Update

This article mainly talks about "what is the structure of python loop". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the structure of the python loop?"

1. Find the greatest common divisor of two numbers num1 = int (input ('please enter the first number:') num2 = int (input ('please enter the second number:') max_num = max (num1, num2) min_num = min (num1, num2) r = max_num% min_numwhile r! = 0: max_num = min_num min_num = r = max_num% min_numprint (num1, "and", num2, "the greatest common divisor is" Min_num) 2. Integer reversal: such as 12345 Output 54321#1num1 = input ('Please enter a number:') lenth = len (num1) a = [] for i in num1: a.append (I) a.reverse () # reverse the list str1 =''for i in a: str1 + = iprint (int (str1)) # 2number_new=num1 [::-1] # slice print (number_new) # 3new_num = 0num1 = int (num1) while num1: # the remainder of num1 The first loop calculates the variable last = num1% 10 # new_num, putting last in individual bits for the first time, and ten places for the second time, increasing in turn. New_num = new_num * 10 + last # round off the num1 to ensure that the next bit is taken out in the next loop and the integer sum between num1=num1 / / 10print (new_num) 3 and 1: 10 is guaranteed. Get the current number for i in range (21): for j in range (21): if ionomj > 20 and c: print ('{} + {} = {} '.format) 4, enter the daily study time (in hours) from Monday to Friday, and calculate the average daily study time. B = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] sum = 0for i in range (5): a = int (input (study time of f'{b [I]}:') sum + = apingjun = sum / 5print ("average study time:", pingjun) 5, output a complete number of less than 10000

If a positive integer is equal to the sum of all divisors except itself, it is called a complete number.

For example, 6 is the first perfect number, because 6, 1, 2, 3.

For i in range (1 for j in range 1000): sum = 1 for j in range (1 Magi): if I% j = 0: sum + = j if sum = = I: print (f "{I} is a complete number") 6, users play games

Each time you play 5 games (the results are shown below) (1) less than 5 games cannot be promoted. (2) in 5 games, if 80% reaches more than 80 points, it is level 1, and if 60% reaches 80 points or more, it is level 2, otherwise it cannot be promoted.

C = [] a = int (input ('you are playing the first inning, the score is:') c.append (a) for i in range (4): B = input ('continue to play (yline n):') if b ='y play: print ('play the next round') a = int (input (f 'you are playing the first round, score:') c.append (a) else: print ('I'm sorry You failed to finish the game') breaksum = 0for i in c: if I > = 80: sum + = 1if sum / len (c) > = 0.8: print ('level 1') elif sum / len (c) > = 0.6: print ('level 2') else: print ('cannot be promoted') 7, automatic menu loop

As long as you do not enter 3, cycle welcome to the xxx system 1 login 2 registration 3 exit please select: 1 login

A = Truewhile a: print ('Welcome to the student management system') print ('1 login 2 register 3 exit') num = int ('please select:') if num = 3: print ("exit system") a = False elif num = = 1: print ('Congratulations on login') elif num = = 2: print ('Congratulations on registration') 8, print graphics

one

twenty-one

three hundred and twenty one

4321

54321

654321

For i in range (1B7): for j in range (I): print (iMuj enduring cycles') print () so far, I believe you have a better understanding of "what is the structure of python loops". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Loop learning input full time login structure number integer system maximum success individual digit common divisor content score maximum common divisor being first time output Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MySQL Huawei NVidia Redmi