鱼C论坛

 找回密码
 立即注册
查看: 1744|回复: 7

求修改,讲一下

[复制链接]
发表于 2022-2-15 22:31:18 From FishC Mobile | 显示全部楼层 |阅读模式
30鱼币
def demo(score):
    if score < 60:
        return "D"
    if 60 <= score < 80:
        return "C"
    if 80 <= score < 90:
        return "B"
    if 90 <= score < 100:
        return "A"
    if score == 100:
        return "S"
    else:
        return "请输入正确形式"
   

while True:
    score = int(input("请输入分数: "))
    a = demo(score)
    if demo(score) != "请输入正确形式":
            print("你的等级是:%s"%a)            
    else:
            print("请输入0 ~ 100以内的数字")            
    temp = input("是否继续进行测试,继续请按y")
    if temp == "y":
        print("测试继续...")
        continue
    else:
        print("测试结束!!!")
        break
        
        

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-2-15 22:36:21 | 显示全部楼层
本帖最后由 isdkz 于 2022-2-15 23:04 编辑
def demo(score):
    if 0 <= score < 60:                            # 这里加个大于等于0,不然它小于0也会输出D
        return "D"
    if 60 <= score < 80:
        return "C"
    if 80 <= score < 90:
        return "B"
    if 90 <= score < 100:
        return "A"
    if score == 100:
        return "S"
    else:
        return "请输入正确形式"
   

while True:
    score = input("请输入分数: ")                      # 先不要转成整数
    if not score.isdigit():                                 # 对输入先判断一下
        print('请输入一个整数')
        continue
    score = int(score)
    a = demo(score)
    if demo(score) != "请输入正确形式":
            print("你的等级是:%s"%a)            
    else:
            print("请输入0 ~ 100以内的数字")            
    temp = input("是否继续进行测试,继续请按y")
    if temp.lower() == "y":                               # 这里可以先把它处理一下再判断是不是小写y,因为别人有可能输入大写
        print("测试继续...")
        continue
    else:
        print("测试结束!!!")
        break
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-2-15 22:45:05 From FishC Mobile | 显示全部楼层
isdkz 发表于 2022-2-15 22:36

如果输入a它显示有错误,无法转化为字符串a
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-2-15 22:51:48 | 显示全部楼层
本帖最后由 isdkz 于 2022-2-15 23:01 编辑
蜜雪冰城 发表于 2022-2-15 22:45
如果输入a它显示有错误,无法转化为字符串a


那你可以先用字符串的isdigit方法判断一下,我改一下上面的代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-2-18 19:04:46 | 显示全部楼层
本帖最后由 饭-米粒 于 2022-2-18 19:12 编辑
def tmp007():
    result = [100,90,80,60,0]
    rank =   ['S','A','B','C','D']
    def demo(num):
        if num >100 or num <0:
            return False
        else:
            for i in range(0,len(result)):
                if result[i]<= num:
                    return(rank[i])
    while(True):
        sore = input("请输入分数:")
        if sore.isdigit(): 
            item = demo(int(sore))
            if item == False:
                print("请输入0~100以内的数字:")
                continue
            else:
                print("你的等级是%s"%item)
            temp = input("是否继续进行测试,继续请输入Y")
            if temp =='y' or temp =='Y':
                continue
            break
        else:
            print("请输入0~100以内的数字:")
tmp007() 

这种类型可以用这种方法,感觉还不错,昨天在网上看到的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-2-19 22:08:27 | 显示全部楼层
def tmp007():
    result = [100,90,80,60,0]
    rank = ['S','A','B','C','D']
    def demo(num):
        if num >100 or num <0:
            return False
        else:
            for i in range(0,len(result)):
                if result[i]<= num:
                    return(rank[i])
    while(True):
        sore = input("请输入你的分数:")
        if sore.isdigit():
            item = demo(int(sore))
            if item == False:
                print("请输入0~100的数字:")
                continue
            else:
                print("级别是%s"%item)
            temp = input("是否继续进行,继续请输Y")
            if temp =='y' or temp =='Y':
                continue
            break
        else:
            print("请输入0~100的数字:")
tmp007()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-2-19 22:09:40 | 显示全部楼层
cs上的帖子,你试试
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-2-19 23:10:29 | 显示全部楼层
纯粹练习练习:
Python
grade = lambda score: 'S' if score == 100 else 'A' if score >= 90 else 'B' if score >= 80 else 'C' if score >= 60 else 'D'
res = 'y'
while res == 'y':
    print("你的等级是:", grade(int(input("请输入分数: "))), "\n是否继续进行测试?(y,n)")
    res = input()
    print("测试继续..." if res == 'y' else "测试结束!!!")

C++
#include <iostream>
using namespace std;

auto grade = [](int score) -> char{ return score == 100 ? 'S' : score >= 90 ? 'A' : score >= 80 ? 'B' : score >= 60 ? 'C' : 'D'; };

int main()
{
    int score;
    for(char c = 'y'; c == 'y';){
        cout << "请输入分数: ";
        cin >> score;
        cout << "你的等级是:" << grade(score) << endl << "是否继续进行测试?(y,n)";
        cin >> c;
        c != 'y' ? cout << "测试结束!!!" << endl : cout << "测试继续..." << endl;
    }
    return 0;
}
C
#include <stdio.h>

char grade(int score){ return score == 100 ? 'S' : score >= 90 ? 'A' : score >= 80 ? 'B' : score >= 60 ? 'C' : 'D'; }

int main()
{
    int score;
    for(char c = 'y'; c == 'y';){
        printf("请输入分数: ");
        scanf("%d", &score);
        getchar();
        printf("你的等级是:%c\n是否继续进行测试?(y,n)", grade(score));
        scanf("%c", &c);
        printf(c != 'y' ? "测试结束!!!" : "测试继续...\n");
    }
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 08:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表