python 输入和输出

使用print() 函数输出

1、基本语法格式

print(输出内容)

a = 100                                     # 变量a,值为100
b = 5                                       # 变量b,值为5
print(9)                                    # 输出数字9
print(a)                                    # 输出变量a的值100
print(a*b)                                  # 输出 a*b的结果500
print("go big or go home")                # 输出要么出众,要么出局“go big or go home”

2、输出多个内容

print(a,b,'要么出众,要么出局')

3、ASKII码形式输出

print('a')                                     # 输出字符a
print(chr(97))                                 # 输出字符a
print('A')                                     # 输出字符A
print(chr(65))                                 # 输出字符A
print('B')                                     # 输出字符B
print(chr(66))                                 # 输出字符B
print('+')                                     # 输出字符+
print(chr(43))                                 # 输出字符+
print(8)                                       # 输出字符8
print(chr(56))                                 # 输出字符8
print('[')                                     # 输出字符[
print(chr(91))                                 # 输出字符[
print(']')                                     # 输出字符]
print(chr(93))                                 # 输出字符]

4、输出到文件

fp = open(r'D:\mr.txt','a+')               # 打开文件
print("要么出众,要么出局。",file=fp)       # 输出到文件中
fp.close()                                   # 关闭文件

在这里插入图片描述

5、输出日期

import datetime                                      # 调用日期模块datetime
print('当前年份:'+str(datetime.datetime.now().year))   # 输出当前年份,当年是2018年,输出2018
print('当前日期时间:'+datetime.datetime.now().strftime('%y-%m-%d %H:%M:%S'))
# #输出当前日期和时间,如:18-11-20 15:30:23,注意代码中的单引号,大小写,不能写错

使用input()函数输入

variable=input(“提示文字”)

示例1

import datetime                                  # 调入时间模块
imyear = input("请输入您的出生年份:")           # 输入出生年份,必须是4位数字的,如1982
nowyear= datetime.datetime.now().year           #计算当前年份
age= nowyear- int(imyear)                       # 用于计算实际年龄
print("您的年龄为:"+str(age ) +"岁")            # 输出年龄
# 根据计算的年龄判断所处的年龄阶段,判定标准是根据联合国组织给出的新年龄分段判定标准
if age<18:                                       #如果年龄小于18岁
     print("您现在为未成年人 ~@_@~")             #输出为“您现在为未成年人 ~@_@~”
if age>=18 and age<66:                           #如果年龄大于18岁但小于66岁
     print("您现在为青年 (-_-)")                 #输出为“您现在为青年人 (-_-)”
if age>=66 and age<80:                           #如果年龄大于65岁但小于80岁
     print("您现在为中年人~@_@~")                #输出为“您现在为中年人~@_@~”
if age>=80:                                      #如果年龄大于或等于80岁
     print("您现在为老年人 *-_-* ")              #输出为“您现在为老年人*-_-* ”

示例二

height=float(input("请输入您的身高:"))  # 输入身高
weight=float(input("请输入您的体重:"))  # 输入体重
bmi=weight/(height*height)      # 计算BMI指数

# 判断身材是否合理
if bmi<18.5:
    print("您的BMI指数为:"+str(bmi))  #输出BMI指数
    print("体重过轻 ~@_@~")
if bmi>=18.5 and bmi<24.9:
    print("您的BMI指数为:"+str(bmi))  #输出BMI指数
    print("正常范围,注意保持 (-_-)")
if bmi>=24.9 and bmi<29.9:
    print("您的BMI指数为:"+str(bmi))  #输出BMI指数
    print("体重过重 ~@_@~")
if bmi>=29.9:
    print("您的BMI指数为:"+str(bmi))  #输出BMI指数
    print("肥胖 ^@_@^")

猜你喜欢

转载自blog.csdn.net/imxlw00/article/details/84979058