Python - 程序体验

 

 

CASE:输入某年某月某日,判断这一天是这一年的第几天?

# 输入某年某月某日, 判断这一天是这一年的第几天?
import datetime

dtstr = input('Enter the datetime:(20240124):')
dt = datetime.datetime.strptime(dtstr, "%Y%m%d")
another_dtstr = dtstr[:4] + '0101'
another_dt = datetime.datetime.strptime(another_dtstr, "%Y%m%d")
print(int((dt - another_dt).days) + 1)

 

 

CASE:输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数?

# 输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
import string

s = input('input a string:')
letter = 0
space = 0
digit = 0
other = 0
for c in s:
    if c.isalpha():
        letter+=1
    elif c.isspace():
        space+=1
    elif c.isdigit():
        digit+=1
    else:
        other+=1
print('There are %d letters,%d spaces,%d digits \
and %d other characters in your string.'\ 
     % (letter,space,digit,other))

 

 

CASE:input的用法

print("欢迎来到上海欢乐谷,儿童免费,成人收费。")
age = int(input("请输入你的年龄: "))
if age >= 18:
    print("您已成年,游玩需要补票10元。")
print("祝您游玩愉快")

 

posted @ 2024-01-24 23:15  HOUHUILIN  阅读(2)  评论(0编辑  收藏  举报