python Cycle Structure Exercise

  • 2021-12-12 08:52:33
  • OfStack

Directory 1, find the greatest common divisor 2 of two numbers, and integer inversion: for example, 12345, output 543213, add integers between 1 and 10, and get the current number 4 with accumulated value greater than 20. Input the daily learning time (in hours) from Monday to Friday, and calculate the average daily learning time. 5. Output the complete number below 10000. 6. Users play games. 7. Menu automatically loops. 8. Print graphics

1. Find the greatest common divisor of two numbers


num1 = int(input(' Please enter the 1 Number :'))
num2 = int(input(' Please enter the 2 Number :'))
max_num = max(num1, num2)
min_num = min(num1, num2)
r = max_num % min_num
while r != 0:
    max_num = min_num
    min_num = r
    r = max_num % min_num
print(num1, " And ", num2, " The greatest common divisor of is ", min_num)

2. Integer inversion: For example, 12345, output 54321


#1
num1 = input(' Please enter 1 Number :')
lenth = len(num1)
a = []
for i in num1:
    a.append(i)
a.reverse()    # Invert a list 
str1 =''
for i in a:
    str1 += i
print(int(str1))

#2
number_new=num1[::-1]     # Slice 
print(number_new)

#3
new_num = 0
num1 = int(num1)
while num1 :
   # Right num1 Find the remainder, the first 1 The secondary cycle finds out the single bit 
   last = num1 % 10
   #new_num This variable, the first 1 Second general last Put it in a place, number one 2 Second place 10 Bits, incrementing in turn. 
   new_num = new_num * 10 + last
   # Right num1 After seeking the surplus, round it up and guarantee it under 1 Be in a lower position 1 Cycles are taken out 
   num1=num1 // 10
print(new_num)

3. Integers between 1 and 10 are added to obtain the current number whose accumulated value is greater than 20


for i in range(21):
   for j in range(21):
      if i + j > 20 and c:
         print('{} + {} = {}'.format(i,j,i+j))

4. Enter the daily study time (in hours) from Monday to Friday, and calculate the average daily study time.


b = [' Week 1',' Week 2',' Week 3',' Week 4',' Week 5']
sum = 0
for i in range(5):
   a = int(input(f'{b[i]} Study time :'))
   sum += a
pingjun = sum / 5
print(" Average learning time :",pingjun)

5. Output perfect numbers below 10000

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

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


for i in range(1,1000):
   sum = 1
   for j in range(1,i):
      if i % j == 0:
         sum += j

   if sum == i:
      print(f"{i} Is a perfect number ")

6. Users play games

Play 5 games at a time (the renderings are as follows) (1) You can't advance if you don't play 5 games (2) In 5 games, if 80% reaches 80 points or more, it is Grade 1, and if 60% reaches 80 points or more, it is Grade 2, otherwise you can't advance


c = []
a = int(input(' You are playing the first 1 Bureau, the result is :'))

c.append(a)
for i in range(4):
   b = input(' Keep playing (y,n):')
   if b == 'y':
      print(' Go down 1 Bureau ')
      a = int(input(f' You are playing the first {i+2} Bureau, the result is :'))
      c.append(a)
   else:
      print(' It's a pity that you didn't finish the game ')
      break
sum = 0
for i in c:
   if i >= 80:
     sum += 1
if sum / len(c) >= 0.8:
   print('1 Grade ')
elif sum / len(c) >= 0.6:
   print('2 Grade ')
else:
   print(' Can't advance ')

7. The menu automatically loops

As long as you don't enter 3, welcome to xxx system 1 login 2 registration 3 exit please select: 1 login


a = True
while a:
   print(' Welcome to Student Management System ')
   print('1 Login  2 Registration  3 Quit  ')
   num = int(input(' Please select :'))
   if num == 3:
      print(" Exit the system ")
      a = False
   elif num == 1:
      print(' Congratulations on your successful login ')
   elif num == 2:
      print(' Congratulations on successful registration ')

8. Print graphics

1
21
321
4321
54321
654321


for i in range(1,7):
   for j in range(i):
      print(i-j,end='')
   print()


Related articles: