鱼C论坛

 找回密码
 立即注册
查看: 2035|回复: 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
        
        

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

  15. while True:
  16.     score = input("请输入分数: ")                      # 先不要转成整数
  17.     if not score.isdigit():                                 # 对输入先判断一下
  18.         print('请输入一个整数')
  19.         continue
  20.     score = int(score)
  21.     a = demo(score)
  22.     if demo(score) != "请输入正确形式":
  23.             print("你的等级是:%s"%a)            
  24.     else:
  25.             print("请输入0 ~ 100以内的数字")            
  26.     temp = input("是否继续进行测试,继续请按y")
  27.     if temp.lower() == "y":                               # 这里可以先把它处理一下再判断是不是小写y,因为别人有可能输入大写
  28.         print("测试继续...")
  29.         continue
  30.     else:
  31.         print("测试结束!!!")
  32.         break
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

如果输入a它显示有错误,无法转化为字符串a
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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


那你可以先用字符串的isdigit方法判断一下,我改一下上面的代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-18 19:04:46 | 显示全部楼层
本帖最后由 饭-米粒 于 2022-2-18 19:12 编辑
  1. def tmp007():
  2.     result = [100,90,80,60,0]
  3.     rank =   ['S','A','B','C','D']
  4.     def demo(num):
  5.         if num >100 or num <0:
  6.             return False
  7.         else:
  8.             for i in range(0,len(result)):
  9.                 if result[i]<= num:
  10.                     return(rank[i])
  11.     while(True):
  12.         sore = input("请输入分数:")
  13.         if sore.isdigit():
  14.             item = demo(int(sore))
  15.             if item == False:
  16.                 print("请输入0~100以内的数字:")
  17.                 continue
  18.             else:
  19.                 print("你的等级是%s"%item)
  20.             temp = input("是否继续进行测试,继续请输入Y")
  21.             if temp =='y' or temp =='Y':
  22.                 continue
  23.             break
  24.         else:
  25.             print("请输入0~100以内的数字:")
  26. tmp007()
复制代码


这种类型可以用这种方法,感觉还不错,昨天在网上看到的
小甲鱼最新课程 -> https://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()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-19 22:09:40 | 显示全部楼层
cs上的帖子,你试试
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

C++
  1. #include <iostream>
  2. using namespace std;

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

  4. int main()
  5. {
  6.     int score;
  7.     for(char c = 'y'; c == 'y';){
  8.         cout << "请输入分数: ";
  9.         cin >> score;
  10.         cout << "你的等级是:" << grade(score) << endl << "是否继续进行测试?(y,n)";
  11.         cin >> c;
  12.         c != 'y' ? cout << "测试结束!!!" << endl : cout << "测试继续..." << endl;
  13.     }
  14.     return 0;
  15. }
复制代码
C
  1. #include <stdio.h>

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

  3. int main()
  4. {
  5.     int score;
  6.     for(char c = 'y'; c == 'y';){
  7.         printf("请输入分数: ");
  8.         scanf("%d", &score);
  9.         getchar();
  10.         printf("你的等级是:%c\n是否继续进行测试?(y,n)", grade(score));
  11.         scanf("%c", &c);
  12.         printf(c != 'y' ? "测试结束!!!" : "测试继续...\n");
  13.     }
  14.     return 0;
  15. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-30 04:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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