用Python语言设计程序,5道题,谢谢!

Python015

用Python语言设计程序,5道题,谢谢!,第1张

第一题,用input()接受用户输入,int()把输入转换成整型,if... else语句判断条件。能被2整除的是偶数,否则奇数

代码

n = int(input())

if n % 2 == 0:

..#前面的点表示缩进。

..print("是偶数")

else:

..print("是奇数")

第二题input()、int()和嵌套if语句

代码:

n = int(input())

if n % 100 != 0:

..if n % 4 == 0:

....print("是闰年")

..else:

....print("是平年")

elif n % 400 == 0:

..print("是闰年")

else:

..print("是平年")

第三题还是一样滴,用input(),int()和if...else语句

代码:

a = int(input())

b = int(input())

if a >b:

..print(a – b)

else:

..print(b – a)

第四题也是一样滴

代码:

m = int(input())

n = int(input())

if m <n:

..print(m, n)

else:

..print(n, m)

第五题 (*^▽^*)

代码:

a = int(input())

if a >=60:

..print("合格")

else:

..print("不合格")

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

程序源代码

实例

#!/usr/bin/python

# -*- coding: UTF-8 -*-

score = int(raw_input('输入分数:\n'))

if score >= 90:

grade = 'A'

elif score >= 60:

grade = 'B'

else:

grade = 'C'

print '%d 属于 %s' % (score,grade)

以上实例输出结果为:

输入分数:8989 属于 B