1. Find the sum of numbers between 1 and 100 that are not divisible by 3
- Loop condition: i<100
- Loop operation
The implementation code is as follows:
def sums():
sum = 0
for num in range(1, 101):
if num % 3 != 0:
sum += num
print("1~100之间不能被3整除的数之和为:%s" % (sum))
sums()
print("1~100之间不能被3整除的数之和为:%s" % (sum))
operation result:
2. Calculate the sum of odd numbers within 100
- Calculate the sum of odd numbers within 100 , set a breakpoint to debug the program, and track the execution order of the three expressions and the change of the loop variable
The implementation code is as follows:
sum = 0
n = 99
while n > 0:
sum = sum + n
n = n - 2
print('100以内的奇数之和为:%s' % (sum))
operation result:
3. Calculate the proportion of customers
- Shopping malls conduct surveys on the age level of customers
Calculate the proportion of customers in each age group
The implementation code is as follows:
num=0
num2=0
for i in range(1,11):
age = int(input('请输入第%d位顾客的年龄:'%i))
if age>=30:
num=num+1
else:
num2=num2+1
a=num/10*100
b=num2/10*100
print('30岁以下的比例是%.1f%%'%b)
print('30岁以上的比例是%.1f%%'%a)
operation result:
Four, cycle accumulation
- Add integers between 1 and 10 to get the current number whose accumulated value is greater than 20
- hint
- Use a loop to add up from 1 to 10
- Determine whether the cumulative value is greater than 20
- If it is greater than 20, break out of the loop and print the current value
The implementation code is as follows:
sum =0
for i in range(1,11):
sum+= i
if sum>20:
print('1~10之间的整数相加,得到累加值大于20的当前数有:%s' % sum)
operation result:
5. Accumulation of even numbers
- Find the sum of all even numbers between 1 and 10
- hint
- Use a loop to accumulate, the range of the loop is from 1 to 10
- Check if the current number is even
If it is an odd number skip, execute the next cycle. If even, add up
The implementation code is as follows:
sum_1 = 0
for i in range(1,11):
if (i % 2==0):
sum_1=sum_1+i
print ("1~10之间的所有偶数和为%d" % sum_1)
operation result:
6. Cyclic entry of member information
- training points
- for loop structure
- continue statement
- Statement of needs
- Circular entry of information for 3 members
- If the membership number is valid, the input information will be displayed; otherwise, the input failure will be displayed
- Implementation ideas
- Analysis problem: there are repeated operations and the number of repetitions is determined
- Circular entry of 3 member information
- The membership number is invalid, use continue to realize the program jump
- use continue statement
The implementation code is as follows:
text1 = input('MyShopping管理系统 》 客户信息管理 》 添加客户信息')
text2 = input('请输入会员号(<4位整数>):')
a = int(text2)
for i in range(0,1):
if 1000 <= a <= 9999:
text3 = input('请输入会员生日(月/日<用两位数表示>):')
text4 = input('请输入积分:')
print("已录入的会员信息是:" + text2, text3, text4)
continue
for i in range(0,2):
if 1000 <= a <= 9999:
text2 = input('请输入会员号(<4位整数>):')
text3 = input('请输入会员生日(月/日<用两位数表示>):')
text4 = input('请输入积分:')
print("已录入的会员信息是:" + text2, text3, text4)
continue
operation result:
7. Verify user login information
User login verification, the number of verifications is up to 3 times
The implementation code is as follows:
Account = 'admin'
Password = 123
test1='MyShopping系统'
for i in range(0,3):
a = input('输入您的账户:')
p = eval(input('输入您的密码:'))
if(a == Account and p == Password):
i = i + 1
print('欢迎登录'+test1+'!')
break
elif(i < 2):
i = i + 1
print('账户或密码有误,您还有%d次机会' %(3-i))
else:
print('账户或密码有误,3次机会已用完,请明天再登录,退出!')
operation result: