测试一个变量是否会根据两一个变量变化而变化。
name = "Li QW" #定义变量
fname = name #fname等于name变量产生的值
print("My name is " , name,fname)
name = "study"
print(name,fname) #由于第一次已经确定了fname值,所以fname不会再根据name的变化而变化。
结果输出:
My name is Li QW Li QW
study Li QW
在这里三个引号有打印多行的功能
msg='''
name = "Li QW"
fname = name
print("My name is " , name,fname)
name = "study"
'''
print(msg)
结果输出:
name = "Li QW"
fname = name
print("My name is " , name,fname)
name = "study"
用户交互1:
输入name/age/job,最后打印
name = input("name:")
age = input("age:")
job = input("job:")
info3 = '''
---info3 of '''+ name +''' --- 这里用的是字符串的拼接,效率最低
name:''' + name +'''
age:''' + age +'''
job:''' + job +'''
'''
print(info3)
结果输出:
name:li
age:20
job:IT
---info3 of li ---
name:li
age:20
job:IT
用户交互2:优化版
name = input("name:")
age = int(input("age:")) #这里设定了输入类型,只能为数字,输入其它符号就会报错
print(type(age)) #打印age的类型
job = input("job:")
info = '''
----info of %s ---- #%s在这里相当于占位符的意思
name:%s # s = string 字符串
age:%d # d = digit 只能接收数字
job:%s # f = float 浮点(小数)
''' % (name,name,age,job)
print(info)
结果输出:
name:li
age:20
<class 'int'>
job:it
----info of li ----
name:li
age:20
job:it
错误输出:
name:li
age:abc
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/untitled1/one_21/week1/interaction2.py", line 5, in <module>
age = int(input("age:"))
ValueError: invalid literal for int() with base 10: 'abc' 这里报错提示,age需要输入int类型。