程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Simple exercises of Python loop structure

編輯:Python

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_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 : Such as 12345, Output 54321

#1
num1 = input(' Please enter a number :')
lenth = len(num1)
a = []
for i in num1:
a.append(i)
a.reverse() # Reverse list 
str1 =''
for i in a:
str1 += i
print(int(str1))
#2
number_new=num1[::-1] # section 
print(number_new)
#3
new_num = 0
num1 = int(num1)
while num1 :
# Yes num1 Mod , In the first loop, the bit is found 
last = num1 % 10
#new_num This variable , For the first time last Put it in one place , For the second time, put it in the tenth place , In turn, increasing .
new_num = new_num * 10 + last
# Yes num1 Round the remainder , Ensure that the next bit is taken out in the next cycle 
num1=num1 // 10
print(new_num)

3、1~10 Add integers between , The cumulative value is greater than 20 The current number of

sum = 0
for i in range(11):
sum += i
if sum > 20:
print(i)
break

4、 Enter the daily study time from Monday to Friday ( In hours ), And calculate the average daily learning time .

b = [' Monday ',' Tuesday ',' Wednesday ',' Thursday ',' Friday ']
sum = 0
for i in range(5):
a = int(input(f'{b[i]} Time for study :'))
sum += a
pingjun = sum / 5
print(" Average learning time :",pingjun)

5、 Output 10000 The following perfect numbers . If a positive integer is equal to the sum of all divisors except itself , Call it a perfect number .
Such as 6 Is the first perfect number , because 6=1+2+3

for i in range(1,1000):
sum = 0
for j in range(1,i):
if i % j == 0:
sum += j
if sum == i:
print(f"{i} It's a complete number ")

6、 Users play games , Every play 5 game ( The renderings are as follows ) (1) Insufficient 5 The Bureau cannot be promoted (2) stay 5 In the game , If 80% achieve 80 More than , It's level one , If 60% achieve 80 It is divided into two levels , Otherwise you can't be promoted

c = []
a = int(input(' You are playing the first game , The result is :'))
c.append(a)
for i in range(4):
b = input(' Keep playing (y,n):')
if b == 'y':
print(' Go on to the next game ')
a = int(input(f' You are playing the {i+2} game , The result is :'))
c.append(a)
else:
print(' unfortunately , 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(' Class A ')
elif sum / len(c) >= 0.6:
print(' second level ')
else:
print(' Can't advance ')

7、 The menu automatically cycles , As long as you don't enter 3, Just cycle Welcome to xxx System 1 Sign in 2 register 3 sign out Please select :1 Sign in

a = True
while a:
print(' Welcome to the student management system ')
print('1 Sign in 2 register 3 sign out ')
num = int(input(' Please select :'))
if num == 3:
print(" Exit the system ")
a = False
elif num == 1:
print(' Congratulations on login success ')
elif num == 2:
print(' Congratulations on your 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()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved