菜鸟教程 -- 学的不仅是技术,更是梦想!

Python 基础教程
(追記) (追記ここまで)

Python 练习实例15

Python 100例 Python 100例

题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

程序分析:程序分析:(a>b) ? a:b 这是条件运算符的基本例子。

程序源代码:

实例(Python 2.x)

#!/usr/bin/python# -*- coding: UTF-8 -*-score = int(raw_input('输入分数:\n'))ifscore >= 90: grade = 'A'elifscore >= 60: grade = 'B'else: grade = 'C'print'%d 属于 %s' % (score,grade)

实例(Python 3.x)

#!/usr/bin/python3score = int(input('输入分数:\n'))ifscore >= 90: grade = 'A'elifscore >= 60: grade = 'B'else: grade = 'C'print('%d 属于 %s' % (score,grade))

以上实例输出结果为:

输入分数:
89
89 属于 B

Python 100例 Python 100例

AI 思考中...

5 篇笔记 写笔记

  1. #0

    james

    214***[email protected]

    8

    使用 range:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    def k(x):
     if x in range(60):
     print('C')
     elif x in range(60,90):
     print('B')
     else:
     print('A')
    score = int(raw_input('输入分数:\n'))
    k(score)
    

    james

    214***[email protected]

    9年前 (2017年04月26日)
  2. #0

    山下有书

    huj***[email protected]

    3

    参考方法:

    #!/user/bin/python
    # -*- coding: UTF-8 -*-
    a=int(raw_input('输入分数:'))
    # 第一种方法
    print 'A' if a>89 else ('B' if a>59 else 'C')
    # 第二种方法
    print 'A' and a>89 or 'B' and a>59 or 'C'

    山下有书

    huj***[email protected]

    9年前 (2017年05月07日)
  3. #0

    有书

    123***6.com

    9

    参考方法:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    i= int(input('请输入成绩:'))
    ar= [90,60,0]
    res= ['A','B','C']
    for idx in range (0,3):
     if i >=ar[idx]:
     print(res[idx])
     break

    有书

    123***6.com

    9年前 (2017年05月10日)
  4. #0

    逸章

    me@***gdewen.com

    22

    输入在0-100的前提下:

    # coding:utf-8
    score = int(raw_input('输入分数:\n'))
    print(['C','C','B','A'][score/30])
    

    逸章

    me@***gdewen.com

    9年前 (2017年06月13日)
  5. #0

    neuqsc

    186***[email protected]

    7

    参考方法,兼容Python3.x与Python2.x:

    # -*- coding: UTF-8 -*-
    score = [80,78,90,48,62] #5个同学的学习成绩
    grade = [] #用来存放评级
    for i in range(5):
     s = (score[i]>=90)and 'A' or ((score[i]>=60)and 'B' or 'C')
     grade.append(s)
    print (grade)

    neuqsc

    186***[email protected]

    9年前 (2017年10月12日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /