当前位置: 移动技术网 > IT编程>脚本编程>Python > day1-变量、循环、字符编码

day1-变量、循环、字符编码

2018年03月29日  | 移动技术网IT编程  | 我要评论

邪魅总裁的一吻恋人,kilinee,东莞中医院

1.必须介绍一下python吧

Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。

Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。

 

2.进入主题(是不是太快了!?),if-elif-else判断

uname='woshinidaye'
pwd='qunidedaye'

username=input('username:')
password=input('password:')

if username==uname and password==pwd:
    print('welcome %s logging...'%uname)
else:
    print('Incorrect username or password!')

知识点:

---》变量。uname、pwd变量,作为一个对象,存储用户名和密码;

     username、password变量,作为一个对象,存储用户输入的内容。

---》input方法。接受用户输入。

---》if-else判断。

---》%s占位符。使用变量uname填充此位置。

 

2.while循环,来玩个猜数字游戏吧!

age=22
count=0
while count<3:
    guess_age=int(input('guess it:'))
    if guess_age==age:
        print('Yes,you get it!')
        break
    elif guess_age>age:
        print('You have a problem in your eyes,guess younger!')
    else:
        print('Good boy,but guess oldder!')
    count+=1
else:
    print('You have guess too many times,Go away!')

知识点:

---》while循环。count<3,循环三次,跳出循环。

---》if-elif-else判断。

---》break。猜中直接退出循环。

3.for循环。换个姿势继续猜。

--------------别猴急!进入前戏,先来学学for循环----------------------

for i in range(10):
    print(i)

---》最最简单的for循环,循环10次, 打印全部元素。

for i in range(0,10,2):
    print(i)

---》从0开始循环10次,步长为2,隔一打一(隔山打牛)。

for i in range(0,5):
    if i<3:
        print(i)
    elif i==3:
        continue
    else:
        print(i)
    print('hahhaha...')

---》continue作用是跳出本次循环,继续下次循环,所以3不会打印。

for i in range(5):
    print('For i loop'.center(20,'-'),i)
    for j in range(5):
        print('For j loop:',j)
        if j>2:
            break

---》嵌套循环。循环5*4遍,j=3也会打印出,不想打印出“j=3”,那你把条件改成“j==3”啊。。。

 

--------------终于进入主题了,我激动啊!!!----------------------

age=22
for i in range(3):
    guess_age=int(input('guess it:'))
    if guess_age==age:
        print('Yes,you get it!')
        break
    elif guess_age>age:
        print('You have a problem in your eyes,guess younger!')
    else:
        print('Good boy,but guess oldder!')
else:
    print('You have guess too many times,Go away!')

---》和while循环差不多,不需要定义一个变量作为计数器。

4. 三种传参方法,任君选择。

_name=input('what is you name:')
_age=int(input('how old are you:'))
_job=input('what is yourcareer:')
_salary=input('how many you salary:')

#Example one,use ‘%’ placeholder
info='''
---------info for %s----------
name=%s
age=%d
job=%s
salary=%s
''' %(_name,_name,_age,_job,_salary)


#Example tow,use 'format' method
info2='''
---------info for {name}----------
name={name}
age={age}
job={job}
salary={salary}
'''.format(
    name=_name,
    age=_age,
    job=_job,
    salary=_salary
)

#Example three,use 'format' method and placeholder is number.
info3='''
---------info for {0}----------
name={0}
age={1}
job={2}
salary={3}
'''.format(_name,_age,_job,_salary)

print(info3)

---》使用format方法。

---》使用“%”占位符。

5.是时候展示综合实力了。

--------------任性玩----------------------

age=55
count=0
while count<3:
    guess_age=int(input('guess it:'))
    if guess_age==age:
        print('Yes,you get it.')
    elif guess_age>age:
        print('guess younger')
    else:
        print('guess oldder')
    count+=1
    if count==3:
        confirm_continue=input('Are you continue guess:')
        if confirm_continue != 'n':
            count=0 #重新开始循环

6.第一天怎么可以少了Hello World!---打开方式要正确!

name='Hello World!'
print(name)

 

  

  

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网