python-变量、if else语句 、for循环、while循环(4月26号)

变量:python

五.注意:python是可执行程序 在linux写python第一行必须写#!/usr/bin/env python(声明解释器在windows中写python第一行须要写# -*- coding:utf-8 -*-linux

Pycharm:开发调试效率高  在pycharm中new project 而且天天建立一个目录右键diectory命名为day1windows

(1).在day1中建立一个程序  右键new python file命名为var 注意若是想在python中想要有模板  能够在view中点击toolbar点击小工具选项editor选择file and code templates选择python scpipt写上本身想要的模板  点击apply选择okapp

 

(2)能够输出hello 运行工具

六.变量var定义规则:变量名只能是字母、数字或下划线的任意组合oop

                  变量名的第一个字符不能是数字优化

                  如下关键字不能声明为变量明命令行

 

 

 

注释单行#注释多行‘’‘。。。’‘’调试

七.用户输入code

#用户输入三种格式  第一种输入方法
#username = input('username:')
#username = input('password:')
#print(username,password)

#用户输入格式 第二种输入方法
#n1ame = input('name:')
#age = input ('age:')
#job = input('job:')
#salary = input('salary')

#info = '''
#------- info of $ ------

#name:
#age:
#job:
#salary:
#'''

#亦或者是这个用户输入方式
# name = input('name:')
#age = input ('age:')
#job = input('job:')
#salary = input('salary')

#info = '''
#------- info of '''+name+'''------
#name:'''+age+'''
#age:'''+job+'''
#job:'''+salary
#'''

#亦或是第三种方式输入
#name = input('name:')
#age = input ('age:')
#job = input('job:')
#salary = input('salary:')
#info = '''
#------- info of %s ------
#name:%s
#age:%s
#job:%s
#salary:%s
#''' % (name,name,age,job,salary)

#print(info)


#%s=string %d表明只接受数字 帮助监测数据类型 %d再次执行 这样执行会报错not str
#name = input('name:')
#age = input ('age:')
#job = input('job:')
#salary = input('salary:')
#info = '''
#------- info of %s ------
#name:%s
#age:%d
#job:%s
#salary:%s
#''' % (name,name,age,job,salary)

#print(info)

#解决上面数字类型报错问题
name = input('name:')
age = int(input ('age:'))#强制转换成数字 int=integer整形
print(type(age))
job = input('job:')
salary = input('salary:')
info = '''
------- info of %s ------
name:%s
age:%d
job:%s
salary:%s
'''
% (name,name,age,job,salary)

print(info)
#以上执行结果是正确的
#把整形转换成字符串
name = input('name:')
age = int(input ('age:'))#强制转换成数字 int=integer整形
print(type(age)    ,type(   str(age) ))#强制转换成上面那一种
job = input('job:')
salary = input('salary:')
info = '''
------- info of %s ------
name:%s
age:%d
job:%s
salary:%s
'''
% (name,name,age,job,salary)

print(info)
#info2写入用户输入
from os import name

ame = input('name:')
age = input ('age:')
job = input('job:')
salary = input('salary:')
info2 = '''
------- info of  {_name} ------
name:{_name}
age:{_age}
job:{_job}
salary:{_salary}
'''
.format(_name=name,
            _age=age,
            _job=job,
            _salary=salary)

print(info2)#以上是info2的用户输入

 

 

#info3写入方法
from os import name

name = input('name:')
age = input ('age:')
job = input('job:')
salary = input('salary:')
info3 = '''
------- info of  {0} ------
name:{0}
age:{1}
job:{2}
salary:{3}
'''
.format(name,age,job,salary)

print(info3)#以上是info3的用户输入方式

#input的三种格式 info info2 info3
 
#用户密文密码
import getpass(getpass语句)

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

print(username,password)
#密文密码最好用cmd命令行敲出 在pycharm使用不来 命令行是 d: cd 根目录  再dir一下
#而后敲出python var.py(存放密文密码的路径)
 

 

十二.If else流程判断

#十二.if else流程判断
_username = 'hansha'
_password = '1234'
username = input('username:')
password = input('password:')
print(username,password)

if _username == username and _password == password:
    print('welcome user{name}login...'.format(name=username))
else:
    print('invalid username or password!')
#if else猜想年龄
age_of_oldboy = 30
guess_age = int(input('guess age:'))
if guess_age == age_of_oldboy :
    print('yes,you got it.')
elif guess_age > age_of_oldboy:
    print('think smaller...')
else:
    print('think bigger!')

十三.while循环语句

#count = 0
#while True :
#    print('count:',count)
#    count = count +1 #count + =1
#     if count == 1000:
#        break

#
猜想年龄加上while循环语句
age_of_oldboy = 56
count = 0
while True:#while循环语句
   
if count ==3:#可执行三次
       
break#猜想三次结束
   
guess_age = int(input('guess age:'))
    if guess_age == age_of_oldboy :
      print('yes,you got it.')
      break#猜对结束
   
elif guess_age > age_of_oldboy:
        print('think smaller...')
    else:
        print('think bigger!')
    count +=1#while循环结束语句
#while循环语句优化
age_of_oldboy = 56
count = 0
while True:#while循环语句
   
if count <3:#和上段代码比较  优化的语句
       
break#猜想三次结束
   
guess_age = int(input('guess age:'))
    if guess_age == age_of_oldboy :
      print('yes,you got it.')
      break#猜对结束
   
elif guess_age > age_of_oldboy:
        print('think smaller...')
    else:
        print('think bigger!')
    count +=1#while循环结束语句
else
print ('你试了太屡次 已结束')
#while循环改为for循环  摘抄上段while循环语句代码
age_of_oldboy = 56
for i in range(3) :
    guess_age = int(input('guess age:'))
    if guess_age == age_of_oldboy :
      print('yes,you got it.')
      break
    elif
guess_age > age_of_oldboy:
        print('think smaller...')
    else:
        print('think bigger!')
else:
    print ('你试了太屡次 已结束')

 

#for循环隔开循环
for i in range(0,10,2):#0到10 隔开2个数字写出一个
   
print('loop',i)
#循环猜想用户猜错三次是否继续小游戏
age_of_oldboy = 56
count = 0
while count <3:#while循环语句
   
guess_age = int(input('guess age:'))
    if guess_age == age_of_oldboy :
      print('yes,you got it.')
      break#猜对结束
   
elif guess_age > age_of_oldboy:
        print('think smaller...')
    else:
        print('think bigger!')
    count +=1#while循环
   
if count == 3:
        countine_confirm = input ('do you want to keep guessing...?')#当用户已经猜错三次以后询问用户是否继续猜想
       
if countine_confirm != 'n':#!=是不等于 若是用户输入n 肯定不继续
          
count =0
十四. continue and break
#十四continue and break
'''for i in range(0,10):
    if i <3:
        print('loop',i)
    else :
        continue
    print('hehe')#continue
跳出本次循环,继续下一循环 break结束整个循环'''