关于作业的形式理解错误了,重新写一遍,这次主要针对的是上课的内容和笔记。
基础概念:
1、关于变量与常量
在pyhton中,只有变量没有常量,变量名只能是字母、数字和下划线的任意组合,变量名的第一个字符不能是数字,而且变量名不能是汉语和拼音,但是可以用下划线或者大写字母区分长命名。
例如:
name = "jzy"
print("My name is",name)
2.用户交互介绍
(1)简单
name = input('what is your name:') #输入姓名
print(name) #打印
(2)复杂
name = input("name:")
age = int(input("age:"))
job = input("job:")
sal = input("sal:")
info = '''
-----info of %s------
name : %s
age : %s
job : %s
sal :%s
''' %(name,name,age,job,sal)
print(info)
或者
name = input("name:")
age = int(input("age:"))
job = input("job:")
sal = input("sal:")
info = '''
-----info of {_name}------
name : {_name}
age : {_age}
job : {_job}
sal : {_sal}
''' .format(_name=name,
_age=age,
_job=job
_sal=sal)
print(info)
或者
name = input("name:")
age = int(input("age:"))
job = input("job:")
sal = input("sal:")
info = '''
-----info of {0}------
name : {0}
age : {1}
job : {2}
sal : {3}
''' .format(name,age,job,sal)
print(info)
上述三种方法都可以实现多行输出,一般建议使用第一种或者第二种表现形式
3.if...else条件判断
例子:
_username = 'jzy'
_password = '123456'
username = input("username:")
password = input("password:")
if _username == username and _password == password :
print ("welcome user {name}" .format(name=username))
else:
print("invalid user")
4.while循环
例子:
oldboy = 56
count = 0
while count < 3
guess_age = int(input("guess_age:"))
if guess_age == oldboy :
print("you are right")
break
elif guess_age > oldboy :
print("too big")
else :
print("too young")
count +=1
else :
print(“too many times”)
5.for循环
例子:
oldboy = 56
count = 0
for i in range(3) :
guess_age = int(input("guess_age:"))
if guess_age == oldboy :
print("you are right")
break
elif guess_age > oldboy :
print("too big")
else :
print("too young")
else :
print("too many times")