day03_10 注释及简单的用户输入输出
单行注释#
print ("我爱北京天安门")
print ("我爱北京天安门")
#print ("我爱北京天安门")
#print ("我爱北京天安门")
多行注释''' ''' 三个单引号
#!-*- coding:utf-8-*-
'''
print ("我爱北京天安门")
print ("我爱北京天安门")
print ("我爱北京天安门")
print ("我爱北京天安门")
'''
还可以是三个双引号""" """
#!-*- coding:utf-8-*-
"""
print ("我爱北京天安门")
print ("我爱北京天安门")
print ("我爱北京天安门")
print ("我爱北京天安门")
"""
注意:在用python3写代码的时候,print之后的内容需要加上(),不然会报错
input用户交互小程序
#!-*- coding:utf-8-*-
input("your name:")
input("your age:")

#!-*- coding:utf-8-*-
name = input("your name:")
age = input("your age:")
print("您的名字:",name,"您的年龄:",age)

#!-*- coding:utf-8-*-
death_age = 120
name = input("your name:")
age = input("your age:")
print("Your name:", name)
print("You can still live for",death_age-age,"years......")

以上代码出现错误,
--字符串和数字不能拼接在一起
数字转成字符串

需要将代码改成
print("You can still live for",str(death_age-age),"years......")
完整版正确代码
#!-*- coding:utf-8-*-
death_age = 120
name = input("your name:")
age = input("your age:") #input 接收的所有数据都是字符串,即便你输入的是数字,但依然会被当然字符串处理
print( type(age) )
print("Your name:", name)
print("You can still live for",str(death_age-int(age)),"years......")
#int interger = 整数 把字符串转成int,用int(被转的数据)
#str string = 字符串 把数据转成字符串用str(被转换的数据)
#不支持整数,字符串的"-"号拼接运算

其中一代代码换成以下代码也可以执行
print("You can still live for",str(death_age-int(age)),"years......")
如果换成以下代码,会出错,因为字符串和数字不能拼接
print("You can still live for" + death_age-int(age) + "years......")

如果换成以下代码,可以执行,因为都是字符串了
print("You can still live for" + str(death_age-int(age))+ "years......")

, , ,代表三个独立的数据,所以不会出错,
+代表拼接,如果字符串和数字拼接的话,就会出错
posted on 2017-09-06 16:29 darkalex001 阅读(145) 评论(0) 收藏 举报
浙公网安备 33010602011771号