Python用户交互程序

1.单行注释:#

#xxxxx

2.多行注释:'''或""",

'''

xxxxxxxx

'''

3.打印多行:'''或"""

4.打印单行:'或"

'xxxxxxxxxx'

5.输入和格式化拼接输出(最笨的方法是直接用+拼接,但会开辟多块内存空间,效率低下;推荐用以下输出方式:%或format,均只开辟一块内存空间)

name = input("Name:")  #input in python 3.x=raw_input in python 2.x

age = int (input("Age:") )  #integer

info ='''--------------- info of %s -----------------

Name:%s

Age:%d

''' % (name,name,age)

print (info)

另一种格式化输出方式:

info2='''--------------- info of {_name} -----------------

Name:{_name}

Age:{_age}

''' .format(_name=name,

               _age=age)

print (info2)

还可以将变量写成数组:

info3='''--------------- info of {0} -----------------

Name:{0}

Age:{1}

''' .format(name,age)

print (info3)

 

 

posted @ 2017-11-30 01:53  hardy_lu  阅读(279)  评论(0)    收藏  举报