python使用列表模拟10个评委打分,去除最高、低分后,求平均分

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变量相当于控制这件事做多少天

  • 28
    点赞
  • 180
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值