變量: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結束整個循環'''