1、 打分实现

#-*- codeing =utf-8 -*-
#@Author:致远
#@File:test.py
#@Software:PyCharm

scores = [] #定义列表存储分数
#n = int(input("评委人数:"))
for i in range(10):
    score = float(input(f"请输入第{i+1}名评委的打分:"))#输入分数
    while score < 0 or score > 100:
          score = float(input("打分错误,请重新打分:"))
    scores.append(score)#将打分存入列表中
max_score = max(scores)#取最大值
min_score = min(scores)#取最小值
print(f"去掉一个最低分: {min_score}")
scores.remove(min_score)#去最小值
print(f"去掉一个最高分: {max_score}")
scores.remove(max_score)#去最大值
print("该歌手的得分为: %.2f" % (sum(scores) / len(scores)))#总分





  • 定义一个空列表接收评委的打分  :scores = []  
  • 在for循环中接收打分,并对分数进行判断:scores.append(score)#使用append函数将打分存入列表中
  • 判断高低分,然后使用remove函数去掉高低分

2、猜拳实现:

import random
player =int(input('玩家出拳:0-石头,1-剪刀,2-布:'))
computer = random.randint(0,2) //随机生成0~2的整数
print('电脑出拳:%d' % computer)
if ( (player==0)and(computer==1) or (player==1)and(computer==2) or (player==2)and(computer==0) ):
    print('玩家获胜:')
elif player == computer:
    print('平局')

3、1-100偶数累加实现:

法一:

i = 1
sum = 0
while i <= 100:
    if i%2==0:   #判断是否为偶数
        sum = sum + i
    i += 1
print(sum)

法二: 

i = 0 # 初值为0
sum = 0
while i <= 100:
    if i%2==0:
        sum = sum + i
    i += 2     #增量每次加2
print(sum)

 4、退出循环:break(终止整个循环)、continue(跳过循环,执行下一条)

continue:


i = 1
while i <= 5:
    if i == 4:
        print('跳过该次')
        i+=1   #不加此语句,会进入死循环
        continue
    print(i)
    i+=1

break: 

#break
i = 1
while i <= 5:
    if i == 4:
        print('退出循环')
        break
    print(i)
    i+=1

5、嵌套循环:

j=0
while j<5:

        i = 0
        while i<3:
            print('我错了')
            i+=1
        print('写作业')
        print('惩罚结束***************************')

        j+=1

i变量控制每天做某事做多少次,j变量相当于控制这件事做多少天

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐