標籤:pyc number pass welcome break har 知識 無限迴圈 continue
注釋及引號的使用
#我是一行注釋‘‘‘那麼巧, 我也是一行注釋‘‘‘print(‘‘‘列印多行字串-第一行列印多行字串-第二行‘‘‘)print("我在嘗試引號嵌套‘我在嘗試引號嵌套")print(‘那麼巧, 我也在嘗試引號嵌套"我也在嘗試引號嵌套‘)
要求使用者輸入字元, 及字串拼接
注意: 使用者輸入的都為字串, 如需當做數字使用需要進行轉換: int(age)
#要求使用者輸入name=input("what‘s your name?")job=input("what‘s your job?")#列印輸出內容print(name,job)#字串拼接方法1, 盡量不要使用, 要佔多塊記憶體print(‘‘‘---info of ‘‘‘+name+‘‘‘---name:‘‘‘+name+‘‘‘job:‘‘‘+job)#字串拼接方法2, %s代表stringprint(‘‘‘---info of %s---name:%sjob:%s‘‘‘%(name, name, job))#字串拼接方法3print(‘‘‘---info of {_name}---name:{_name}job:{_job}‘‘‘.format(_name=name,_job=job))#字串拼接方法4print(‘‘‘---info of {0}---name:{0}job:{1}‘‘‘.format(name,job))
類比linux登入, 密碼密文展示
注意: 該寫法在pycharm中不不好用
import getpass #引入標準庫password=getpass.getpass("password:")
if...else流程判斷
注意: 由於沒有{ }等結束符, 縮排必須正確
if _username==username and _password==password: print("Welcome {name}".format(name=username))else: print("Invalid username or password")
while迴圈
count=0num=15while count<3: guess=int(input("please a number:")) if guess==num: print("correct!") break elif guess>num: print("Bigger!") count+=1 continue else: print("Smaller!") count+=1 continueelse: print("Guess incorrectly for 3 times!")
無限迴圈
while True: name=input("name:")
for迴圈
for i in range(10): name=input("name:") if name=="bell": breakelse: print("Not correct")
python零基礎學習-基礎知識2-代碼初見