Python輸入與迴圈

來源:互聯網
上載者:User

標籤:輸入   welcome   pycharm   har   pytho   條件   you   range   charm   

python

while迴圈

while 語句:

  執行語句

  結束條件

#應用while輸出1到11counts = 1while True:    print("counts:", counts)    counts = counts +1 #counts +=1    if counts == 11:        break

 for 迴圈

for 變數 in range(起始值,終止值,步長):

  迴圈語句

#從零開始以3為步長不超過10for i in range(0,10,3)    print(i)

python注釋中獲得輸入資訊的幾種方式

方法1:

變數1 = input(“A”)

變數2 = input(“B”)

info = ‘‘‘

C:%s

D:%s

‘‘‘%(變數1,變數2)

#輸入姓名、年齡,輸出姓名、年齡name = input("name")age = int(input("age"))info = ‘‘‘-------------info of %s---------name:%sage:%d‘‘‘%(name,name,age)print(info)

方法2:

變數1 = input(“A”)

變數2 = input(“B”)

info = ‘‘‘

C:{E}

D:{F}

‘‘‘.format(E=變數1,F=變數2)

 
info2 = ‘‘‘-------------info of {_name}---------name:{_name}age:{_age}‘‘‘.format(_name=name,           _age=age)print(info2)

方法3:

變數1 = input(“A”)

變數2 = input(“B”)

info = ‘‘‘

C:{0}

D:{1}

‘‘‘.format(變數1,變數2)

info3 = ‘‘‘-------------info of {0}---------name:{0}age:{1}‘‘‘.format(name, age)print(info3)

 簡單登陸

#引入一個getpassimport getpass#定義一個使用者名稱和密碼_name = ‘123‘_password = ‘456‘#獲得鍵盤輸入值name = input("name")# password = getpass.getpass("password")# 密碼密文,pycharm不好使password = input("password")#判斷使用者名稱和密碼是否一致if _name == name and _password == password:#將使用者名稱傳給輸出語句中    print("welcome user{name} login...".format(name = name))    print("----------------")else:    print("Invalid password")

 猜大小小程式

number = 20#for 迴圈  range(開始,結束,步長)for i in range(1,10,2):    #獲得輸入值    guess_number = int(input("guess number:"))    #進行比較    if guess_number == number:        print("yes, you got it.")    elif guess_number > number:        print("think smaller ...")    else:        print("think bigger!")    print("loop",i)

  

Python輸入與迴圈

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.