Python (averaged score, centered usage is determined whether the input is empty, leap year judgment, the output of the date specified number of days)

1. The averaged results

"""
- 输入学生姓名
- 依次输入学生的三门科目成绩
- 计算该学生的平均成绩,并打印
- 平均成绩保留一位小数
- 计算语文成绩占总成绩的百分比,并打印
"""

name = input("学生姓名:")
Chinese = float(input("语文成绩:"))
Math = float(input("数学成绩:"))
English = float(input("英语成绩:"))

# 总成绩
SumScore = Chinese + Math + English
# 平均成绩
AvgScore = SumScore / 3

ChinesePercent = (Chinese / SumScore) * 100

print('%s 的平均成绩为%.1f' % (name, AvgScore))
print('语文成绩占总成绩的%.2f%%' % ChinesePercent)

Here Insert Picture Description
Here Insert Picture Description

2. center usage

I chose this pycharm to demonstrate:

>>> a = 'hello'
>>> a
'hello'
>>> a.center(40)
'                 hello                  '
>>> a.center(40,'*')
'*****************hello******************'
>>> print("学生管理系统".center(50,'-'))
----------------------学生管理系统----------------------
>>> print("学生管理系统".center(50,'*'))
**********************学生管理系统**********************

Here Insert Picture Description

To determine the user's input is empty

The first:

value = input('Value:')
if value == '':
	print('请输入合法的值')

Here Insert Picture Description

The second:

value = input('Value:')
if not value:
        print('请输入合法的值')

Here Insert Picture Description

Judgment leap year

"""
判断闰年
用户输入年份,判断是否为闰年?
- 能被400整除的是闰年,能被4整除但是不能被100整除的是闰年
"""
year = int(input('Year:'))

if (year % 4 == 0 and year % 100 != 0) \
        or (year % 400 == 0):
    print('%s是闰年' %year)
else:
    print('%s不是闰年' %year)

Here Insert Picture Description

Determines whether the specified number of days in the specified month

Year = int(input("请输入你要输入的年:"))
Month = int(input ("请输入你要输入的月份:"))



if ( Month == 1 or Month == 3 or Month == 5 or Month == 7  or Month == 8 or Month == 10 or Month == 12 ):
        print ( '%d 是31天' %Month )
elif ( Month == 4 or Month == 6 or Month == 9 or Month ==11 ):
        print ( '%d是30天'%Month )
elif (Month == 2 ) and (Year % 4 == 0 and Year % 100 != 0):
       print ('二月是29天')
else:
        print ('二月是28天')

Here Insert Picture Description
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/bmengmeng/article/details/93977246
Recommended