当前位置: 代码迷 >> 综合 >> python-2-基本输入输出
  详细解决方案

python-2-基本输入输出

热度:36   发布时间:2024-02-04 21:34:05.0

''' 基本语法
print(value,sep=" ",end="\n",file=sys.stdout,flush=False)
value:所要输出的数据
sep:输出多个数据时,数据之间的分隔符,默认为空格,可以自定义
end:数据结束时所插入的字符,默认为换行符
file:数据输出位置,默认为sys.stdout(屏幕)
flush:是否清除数据流的缓冲区,预设是不清除

print("输出格式区 % (变量系列区)") 输出格式区可以用字符串代替
%d 整数 %s字符串 %o八进制   %x16进制 %f浮点型数据 
'''
str1="赵富贵";score=100;count=1
print("%s的第%d次成绩如下:" %(str1,count))
print("Chinese = %d\nMath = %d" % (score,score))
count1=2
zifuchuan="%s的第%d次成绩如下:"
print(zifuchuan % (str1,count1))
print("Chinese",score,sep="--",end="\t")
print("Math",score,sep="--")
'''
%(+/-)nd 格式化整数输出  %(+/-)no 格式化8进制整数输出  %(+/-)ns 格式化字符串输出
%(+/-)nx 格式化16进制整数输出  %(+/-)m.nf格式化浮点数输出
-:数据向左靠齐 +:整数显示+号 
浮点型数据 m:保留多少格数据输出 n:小数数据保留格数,其他类型则n是保留多少格子空间
'''
x=100;y=10.5;z="Deep"
print("x=/%+6d/" % x)
print("y=/%6.3f/" % y)
print("z=/%-6s/\n\n\n" % z)

#format()函数
print("format函数")
str2="{}你的第{}次考试成绩是{}"
print(str2.format(str1,count,score))
print("\n\n\n")

#输出数据到文件
'''
file_obj=open(file,mode="r")
file:用字符串列出欲打开的文件
mode:打开文件的模式
r:预设,用于文件的读取
w:打开文件供写入会覆盖原来的内容
a:打开文件写入,但是只是在后边添加数据
x:打开新文件供写入,如果打开文件已经存在会产生错误
b:用二进制的方式打开文件
t:打开txt文件模式,这是默认
+:打开文件供更新用
file_obj:文件对象,可以自定义,不适用时要关闭文件,file_obj.close()
'''
wenjian1=open("g:\wenjian1.txt",mode="w")
print("texting for output",file=wenjian1)   #取代先前的资料
wenjian1.close()
wenjian2=open("g:\wenjian2.txt",mode="a")
print("texting for output",file=wenjian2)   #附加数据后边
wenjian2.close()

print("欢迎使用成绩系统")
clastname=input("请输入中文姓氏:")
cfirstname=input("请输入中文名字:")
cfullname=clastname+cfirstname
lastname=input("please input the lastname:")
firstname=input("please input the firstname:")
fullname=lastname+firstname
print("%s,欢迎使用本系统!\n%s,Wecome to grade system!" % (cfullname,fullname))
english=input("请输入英语成绩:  ")
math=input("请输入数学成绩:  ")
total=(int(english)+int(math))
str=("{},{},你的最终成绩是{}\n\n\n")
print(str.format(cfullname,fullname,total))

dir(__builtins__)  #列出所有内置函数

  相关解决方案