Python基礎1

本節內容2016-05-30php

  1. Python介紹
  2. 發展史
  3. Python 2 0r 3?
  4. 安裝
  5. Hello word程序
  6. 變量
  7. 用戶輸入
  8. 模塊初識
  9. .pyc?
  10. 數據類型初識
  11. 數據運算
  12. if...else語句
  13. 表達式for循環
  14. break and continue
  15. 表達式while循環

1、Ptyhon介紹html

Python誕生於1989年聖誕節期間,創始人吉多•範羅蘇姆(Guido Van Gossum)。java

最新TIOBE排行榜(http://www.tiobe.com/tiobe_index?page=index),Python趕超PHP佔據第五,Python崇尚優美、清晰、簡單,是一個優秀並普遍使用的語言。python

Python能夠應用於衆多領域:數據分析、組件集成、網絡服務、圖像處理、數值計算和科室計算等。linux

目前幾乎全部在中型互聯網企業都在使用Pthon:Youtube、Dropbox、BT、Quora(中國知乎)、Google、Yahoo!、Facebook、NASA、百度、騰訊、汽車之家、美團等。ios

目前Python主要應用領域:c++

  • 雲計算: 雲計算最火的語言, 典型應用OpenStack(雲計算管理平臺)
  • WEB開發: 衆多優秀的WEB框架,衆多大型網站均爲Python開發,Youtube, Dropbox, 豆瓣。。。, 典型WEB框架有Django
  • 科學運算、人工智能: 典型庫NumPy(Python的一種開源的數字擴展,用於存儲和處理大型矩陣), SciPy(一個開源的Python算法庫和數學工具包), Matplotlib(http://matplotlib.org/一個Python的圖形框架), Enthought librarys(Enthought is a global leader in scientific and analytic computing software, training, and consulting, with particular expertise in the Python programming ...),pandas(Python數據分析模塊)
  • 系統運維: 運維人員必備語言
  • 金融:量化交易,金融分析,在金融工程領域,Python不但在用,且用的最多,並且重要性逐年提升。緣由:做爲動態語言的Python,語言結構清晰簡單,庫豐富,成熟穩定,科學計算和統計分析都很牛逼,生產效率遠遠高於c,c++,java,尤爲擅長策略回測
  • 圖形GUI: PyQT, WxPython,TkInte

Python在一些公司的應用: web

  • 谷歌:Google App Engine 、code.google.com 、Google earth 、谷歌爬蟲、Google廣告等項目都在大量使用Python開發
  • CIA: 美國中情局網站就是用Python開發的
  • NASA: 美國航天局(NASA)大量使用Python進行數據分析和運算
  • YouTube:世界上最大的視頻網站YouTube就是用Python開發的
  • Dropbox:美國最大的在線雲存儲網站,所有用Python實現,天天網站處理10億個文件的上傳和下載
  • Instagram:美國最大的圖片分享社交網站,天天超過3千萬張照片被分享,所有用python開發
  • Facebook:大量的基礎庫均經過Python實現的
  • Redhat: 世界上最流行的Linux發行版本中的yum包管理工具就是用python開發的
  • 豆瓣: 公司幾乎全部的業務均是經過Python開發的
  • 知乎: 國內最大的問答社區,經過Python開發(國外Quora)
  • 春雨醫生:國內知名的在線醫療網站是用Python開發的
  • 除上面以外,還有搜狐、金山、騰訊、盛大、網易、百度、阿里、淘寶 、土豆、新浪、果殼等公司都在使用Python完成各類各樣的任務。 

爲何是Python而不是其餘語言?算法

C 和 Python、Java、C#等shell

    C語言: 代碼編譯獲得 機器碼 ,機器碼在處理器上直接執行,每一條指令控制CPU工做

其餘語言: 代碼編譯獲得 字節碼 ,虛擬機執行字節碼並轉換成機器碼再後在處理器上執行

Python 和 C  Python這門語言是由C開發而來

  對於使用:Python的類庫齊全而且使用簡潔,若是要實現一樣的功能,Python 10行代碼能夠解決,C可能就須要100行甚至更多.
  對於速度:Python的運行速度相較與C,絕逼是慢了

Python 和 Java、C#等

  對於使用:Linux原裝Python,其餘語言沒有;以上幾門語言都有很是豐富的類庫支持
  對於速度:Python在速度上可能稍顯遜色

因此,Python和其餘語言沒有什麼本質區別,其餘區別在於:擅長某領域、人才豐富、先入爲主。

Python的種類

  • Cpython

        Python的官方版本,使用C語言實現,使用最爲普遍,CPython實現會將源文件(py文件)轉換成字節碼文件(pyc文件),而後運行在Python虛擬機上。

  • Jyhton

        Python的Java實現,Jython會將Python代碼動態編譯成Java字節碼,而後在JVM上運行。

  • IronPython

        Python的C#實現,IronPython將Python代碼編譯成C#字節碼,而後在CLR上運行。(與Jython相似)

  • PyPy(特殊)

        Python實現的Python,將Python的字節碼字節碼再編譯成機器碼。

  • RubyPython、Brython ...

2、Python發展史 

  • 1989年,爲了打發聖誕節假期,Guido開始寫Python語言的編譯器。Python這個名字,來自Guido所摯愛的電視劇Monty Python’s Flying Circus。他但願這個新的叫作Python的語言,能符合他的理想:創造一種C和shell之間,功能全面,易學易用,可拓展的語言。
  • 1991年,第一個Python編譯器誕生。它是用C語言實現的,並可以調用C語言的庫文件。從一出生,Python已經具備了:類,函數,異常處理,包含表和詞典在內的核心數據類型,以及模塊爲基礎的拓展系統。
  • Granddaddy of Python web frameworks, Zope 1 was released in 1999
  • Python 1.0 - January 1994 增長了 lambda, map, filter and reduce.
  • Python 2.0 - October 16, 2000,加入了內存回收機制,構成了如今Python語言框架的基礎
  • Python 2.4 - November 30, 2004, 同年目前最流行的WEB框架Django 誕生
  • Python 2.5 - September 19, 2006
  • Python 2.6 - October 1, 2008
  • Python 2.7 - July 3, 2010
  • In November 2014, it was announced that Python 2.7 would be supported until 2020, and reaffirmed that there would be no 2.8 release as users were expected to move to Python 3.4+ as soon as possible
  • Python 3.0 - December 3, 2008
  • Python 3.1 - June 27, 2009
  • Python 3.2 - February 20, 2011
  • Python 3.3 - September 29, 2012
  • Python 3.4 - March 16, 2014
  • Python 3.5 - September 13, 2015

3、Python 2 or 3?

In summary : Python 2.x is legacy, Python 3.x is the present and future of the language

Python 3.0 was released in 2008. The final 2.x version 2.7 release came out in mid-2010, with a statement of

extended support for this end-of-life release. The 2.x branch will see no new major releases after that. 3.x is

under active development and has already seen over five years of stable releases, including version 3.3 in 2012,

3.4 in 2014, and 3.5 in 2015. This means that all recent standard library improvements, for example, are only

available by default in Python 3.x.

Guido van Rossum (the original creator of the Python language) decided to clean up Python 2.x properly, with less regard for backwards compatibility than is the case for new releases in the 2.x range. The most drastic improvement is the better Unicode support (with all text strings being Unicode by default) as well as saner bytes/Unicode separation.

Besides, several aspects of the core language (such as print and exec being statements, integers using floor division) have been adjusted to be easier for newcomers to learn and to be more consistent with the rest of the language, and old cruft has been removed (for example, all classes are now new-style, "range()" returns a memory efficient iterable, not a list as in 2.x). 

py2與3的詳細區別

PRINT IS A FUNCTION

The statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old statement (PEP 3105). Examples: 

1 Old: print "The answer is", 2*2 New: print("The answer is", 2*2)
2 Old: print x, # Trailing comma suppresses newline New: print(x, end=" ") # Appends a space instead of a newline
3 Old: print # Prints a newline
4 New: print() # You must call the function!
5 Old: print >>sys.stderr, "fatal error" New: print("fatal error", file=sys.stderr)
6 Old: print (x, y) # prints repr((x, y))
7 New: print((x, y)) # Not the same as print(x, y)!
View Code

 You can also customize the separator between items, e.g.: 

1 print("There are <", 2**32, "> possibilities!", sep="")
View Code

ALL IS UNICODE NOW

還能夠這樣玩: (A,*REST,B)=RANGE(5)

1 <strong>>>> a,*rest,b = range(5)
2 >>> a,rest,b
3 (0, [1, 2, 3], 4)
4 </strong>
View Code

某些庫更名了

Old Name

New Name

_winreg

winreg

ConfigParser

configparser

copy_reg

copyreg

Queue

queue

SocketServer

socketserver

markupbase

_markupbase

repr

reprlib

test.test_support

test.support

  

還有誰不支持PYTHON3?

One popular module that don't yet support Python 3 is Twisted (for networking and other applications). Most

actively maintained libraries have people working on 3.x support. For some libraries, it's more of a priority than

others: Twisted, for example, is mostly focused on production servers, where supporting older versions of

Python is important, let alone supporting a new version that includes major changes to the language. (Twisted is

a prime example of a major package where porting to 3.x is far from trivial 

4、Python安裝

windows

1、下載安裝包
    https://www.python.org/downloads/
2、安裝
    默認安裝路徑:C:\python27
3、配置環境變量
    【右鍵計算機】--》【屬性】--》【高級系統設置】--》【高級】--》【環境變量】--》【在第二個內容框中找到 變量名爲Path 的一行,雙擊】 --> 【Python安裝目錄追加到變值值中,用 ; 分割】
    如:原來的值;C:\python27,切記前面有分號

linux、Mac

 無需安裝,原裝Python環境 ps:若是自帶2.6,請更新至2.7 

5、Hello World程序

在linux 下建立一個文件叫hello.py,並輸入

print("Hello world!")

 

 而後執行命令:python hello.py ,輸出

$ vim hello.py
$ python hello.py
Hello world!

如此一來,執行: ./hello.py 便可。

ps:執行前需給予 hello.py 執行權限,chmod 755 hello.py

1 [root@test ~]# python
2 Python 2.7.11 (default, May 17 2016, 14:00:45) 
3 [GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>> print("Hello world!")
6 Hello world!

對比下其它語言的hello world

1 #include <iostream>
2 int main(void)
3 {
4 std::cout<<"Hello world";
5 }
C++
1 #include <stdio.h>
2 int main(void)
3 {
4 printf("\nhello world!");
5 return 0;
6 }
C
1 public class HelloWorld{
2   // 程序的入口
3   public static void main(String args[]){
4     // 向控制檯輸出信息
5     System.out.println("Hello World!");
6   }
7 }
JAVA
1 <?php  
2             echo "hello world!";  
3 ?> 
PHP
1 puts "Hello world."
RUBY
1 package main
2 
3 import "fmt"
4 
5 func main(){
6 
7     fmt.Printf("Hello World!\n God Bless You!");
8 
9 }
GO

6、變量\字符編碼  

Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.

聲明變量

#_*_coding:utf-8_*_
 
name = "Shawn Yu"

上述代碼聲明瞭一個變量,變量名爲: name,變量name的值爲:"Shawn Yu" 

變量定義的規則:

      • 變量名只能是 字母、數字或下劃線的任意組合
      • 變量名的第一個字符不能是數字
      • 如下關鍵字不能聲明爲變量名
        ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
變量的賦值
name = "Shawn Li"
 
name2 = name
print(name,name2)
 
name = "Jack"
 
print("What is the value of name2 now?")

字符編碼

python解釋器在加載 .py 文件中的代碼時,會對內容進行編碼(默認ascill)

ASCII(American Standard Code for Information Interchange,美國標準信息交換代碼)是基於拉丁字母的一套電腦編碼系統,主要用於顯示現代英語和其餘西歐語言,其最多隻能用 8 位來表示(一個字節),即:2**8 = 256-1,因此,ASCII碼最多隻能表示 255 個符號。

顯然ASCII碼沒法將世界上的各類文字和符號所有表示,因此,就須要新出一種能夠表明全部字符和符號的編碼,即:Unicode

Unicode(統一碼、萬國碼、單一碼)是一種在計算機上使用的字符編碼。Unicode 是爲了解決傳統的字符編碼方案的侷限而產生的,它爲每種語言中的每一個字符設定了統一而且惟一的二進制編碼,規定雖有的字符和符號最少由 16 位來表示(2個字節),即:2 **16 = 65536,
注:此處說的的是最少2個字節,可能更多

UTF-8,是對Unicode編碼的壓縮和優化,他再也不使用最少使用2個字節,而是將全部的字符和符號進行分類:ascii碼中的內容用1個字節保存、歐洲的字符用2個字節保存,東亞的字符用3個字節保存...

因此,python解釋器在加載 .py 文件中的代碼時,會對內容進行編碼(默認ascill),若是是以下代碼的話:

報錯:ascii碼沒法表示中文

1 #!/usr/bin/env python
2   
3 print "你好,世界"

改正:應該顯示的告訴python解釋器,用什麼編碼來執行源代碼,即:

1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3   
4 print "你好,世界"

註釋

  當行注視:# 被註釋內容

  多行註釋:""" 被註釋內容 """

7、用戶輸入

1 #!/usr/bin/env python
2 #_*_coding:utf-8_*_
3  
4  
5 #name = raw_input("What is your name?") #only on python 2.x
6 name = input("What is your name?")
7 print("Hello " + name )

輸入密碼時,若是想要不可見,須要利用getpass 模塊中的 getpass方法,即:

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3   
 4 import getpass
 5   
 6 # 將用戶輸入的內容賦值給 name 變量
 7 pwd = getpass.getpass("請輸入密碼:")
 8   
 9 # 打印輸入的內容
10 print(pwd)

8、模塊初識  

Python的強大之處在於他有很是豐富和強大的標準庫和第三方庫,幾乎你想實現的任何功能都有相應的Python庫支持,之後的課程中會深刻講解經常使用到的各類庫,如今,咱們先來象徵性的學2個簡單的。

sys

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3  
 4 import sys
 5  
 6 print(sys.argv)
 7  
 8  
 9 #輸出
10 $ python test.py helo world
11 ['test.py', 'helo', 'world']  #把執行腳本時傳遞的參數獲取到了

os

1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3  
4 import os
5  
6 os.system("df -h") #調用系統命令

徹底結合一下

1 import os,sys
2  
3 os.system(''.join(sys.argv[1:])) #把用戶的輸入的參數看成一條命令交給os.system來執

本身寫個模塊

python tab補全模塊

9、.pyc是個什麼鬼?

1. Python是一門解釋型語言?

我初學Python時,聽到的關於Python的第一句話就是,Python是一門解釋性語言,我就這樣一直相信下去,直到發現了*.pyc文件的存在。若是是解釋型語言,那麼生成的*.pyc文件是什麼呢?c應該是compiled的縮寫纔對啊!

爲了防止其餘學習Python的人也被這句話誤解,那麼咱們就在文中來澄清下這個問題,而且把一些基礎概念給理清。

  

2. 解釋型語言和編譯型語言 

計算機是不可以識別高級語言的,因此當咱們運行一個高級語言程序的時候,就須要一個「翻譯機」來從事把高級語言轉變成計算機能讀懂的機器語言的過程。這個過程分紅兩類,第一種是編譯,第二種是解釋。

編譯型語言在程序執行以前,先會經過編譯器對程序執行一個編譯的過程,把程序轉變成機器語言。運行時就不須要翻譯,而直接執行就能夠了。最典型的例子就是C語言。

解釋型語言就沒有這個編譯的過程,而是在程序運行的時候,經過解釋器對程序逐行做出解釋,而後直接運行,最典型的例子是Ruby。

經過以上的例子,咱們能夠來總結一下解釋型語言和編譯型語言的優缺點,由於編譯型語言在程序運行以前就已經對程序作出了「翻譯」,因此在運行時就少掉了「翻譯」的過程,因此效率比較高。可是咱們也不能一律而論,一些解釋型語言也能夠經過解釋器的優化來在對程序作出翻譯時對整個程序作出優化,從而在效率上超過編譯型語言。

此外,隨着Java等基於虛擬機的語言的興起,咱們又不能把語言純粹地分紅解釋型和編譯型這兩種。

用Java來舉例,Java首先是經過編譯器編譯成字節碼文件,而後在運行時經過解釋器給解釋成機器文件。因此咱們說Java是一種先編譯後解釋的語言。

 

3. Python究竟是什麼 

其實Python和Java/C#同樣,也是一門基於虛擬機的語言,咱們先來從表面上簡單地瞭解一下Python程序的運行過程吧。

當咱們在命令行中輸入python hello.py時,實際上是激活了Python的「解釋器」,告訴「解釋器」:你要開始工做了。但是在「解釋」以前,其實執行的第一項工做和Java同樣,是編譯。

熟悉Java的同窗能夠想一下咱們在命令行中如何執行一個Java的程序:

javac hello.java

java hello

 

只是咱們在用Eclipse之類的IDE時,將這兩部給融合成了一部而已。其實Python也同樣,當咱們執行python hello.py時,他也同樣執行了這麼一個過程,因此咱們應該這樣來描述Python,Python是一門先編譯後解釋的語言。

4. 簡述Python的運行過程

在說這個問題以前,咱們先來講兩個概念,PyCodeObject和pyc文件。

咱們在硬盤上看到的pyc天然沒必要多說,而其實PyCodeObject則是Python編譯器真正編譯成的結果。咱們先簡單知道就能夠了,繼續向下看。

當python程序運行時,編譯的結果則是保存在位於內存中的PyCodeObject中,當Python程序運行結束時,Python解釋器則將PyCodeObject寫回到pyc文件中。

當python程序第二次運行時,首先程序會在硬盤中尋找pyc文件,若是找到,則直接載入,不然就重複上面的過程。

因此咱們應該這樣來定位PyCodeObject和pyc文件,咱們說pyc文件實際上是PyCodeObject的一種持久化保存方式。

 

10、數據類型初識 

一、數字

2 是一個整數的例子。
長整數 不過是大一些的整數。
3.23和52.3E-4是浮點數的例子。E標記表示10的冪。在這裏,52.3E-4表示52.3 * 10-4。
(-5+4j)和(2.3-4.6j)是複數的例子。

int(整型)

  在32位機器上,整數的位數爲32位,取值範圍爲-2**31~2**31-1,即-2147483648~2147483647
  在64位系統上,整數的位數爲64位,取值範圍爲-2**63~2**63-1,即-9223372036854775808~9223372036854775807
long(長整型)
  跟C語言不一樣,Python的長整數沒有指定位寬,即:Python沒有限制長整數數值的大小,但實際上因爲機器內存有限,咱們使用的長整數數值不可能無限大。
  注意,自從Python2.2起,若是整數發生溢出,Python會自動將整數數據轉換爲長整數,因此現在在長整數數據後面不加字母L也不會致使嚴重後果了。
float(浮點型)
  浮點數用來處理實數,即帶有小數的數字。相似於C語言中的double類型,佔8個字節(64位),其中52位表示底,11位表示指數,剩下的一位表示符號。
complex(複數)
  複數由實數部分和虛數部分組成,通常形式爲x+yj,其中的x是複數的實數部分,y是複數的虛數部分,這裏的x和y都是實數。
注:Python中存在小數字池:-5 ~ 257
 
二、布爾值
  真或假
  1 或 0
三、字符串
"hello world"
萬惡的字符串拼接:
  python中的字符串在C語言中體現爲是一個字符數組,每次建立字符串時候須要在內存中開闢一塊連續的空,而且一旦須要修改字符串的話,就須要再次開闢空間,萬惡的+號每出現一次就會在內從中從新開闢一塊空間。
字符串格式化輸出
1 name = "shawn"
2 print "i am %s " % name
3   
4 #輸出: i am shawn

PS: 字符串是 %s;整數 %d;浮點數%f

字符串經常使用功能:
  • 移除空白
  • 分割
  • 長度
  • 索引
  • 切片
四、列表
建立列表:
1 name_list = ['alex', 'seven', 'eric']
2 3 name_list = list(['alex', 'seven', 'eric'])

基本操做:

  • 索引
  • 切片
  • 追加
  • 刪除
  • 長度
  • 切片
  • 循環
  • 包含
五、元組(不可變列表)
建立元組:
1 ages = (11, 22, 33, 44, 55)
2 3 ages = tuple((11, 22, 33, 44, 55))
六、字典(無序)
建立字典:
1 person = {"name": "mr.wu", 'age': 18}
2 3 person = dict({"name": "mr.wu", 'age': 18})

經常使用操做:

  • 索引
  • 新增
  • 刪除
  • 鍵、值、鍵值對
  • 循環
  • 長度

11、數據運算  

算數運算:

比較運算:

賦值運算:

邏輯運算:

成員運算:

身份運算:

位運算:

運算符優先級:

更多內容:猛擊這裏

  

12、表達式if ... else

場景1、用戶登錄驗證

 1 # 提示輸入用戶名和密碼
 2   
 3 # 驗證用戶名和密碼
 4 #     若是錯誤,則輸出用戶名或密碼錯誤
 5 #     若是成功,則輸出 歡迎,XXX!
 6  
 7  
 8 #!/usr/bin/env python
 9 # -*- coding: encoding -*-
10   
11 import getpass
12   
13   
14 name = raw_input('請輸入用戶名:')
15 pwd = getpass.getpass('請輸入密碼:')
16   
17 if name == "alex" and pwd == "cmd":
18     print("歡迎,alex!")
19 else:
20     print("用戶名和密碼錯誤")

場景2、猜年齡遊戲

在程序裏設定好你的年齡,而後啓動程序讓用戶猜想,用戶輸入後,根據他的輸入提示用戶輸入的是否正確,若是錯誤,提示是猜大了仍是小了

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3  
 4  
 5 my_age = 28
 6  
 7 user_input = int(input("input your guess num:"))
 8  
 9 if user_input == my_age:
10     print("Congratulations, you got it !")
11 elif user_input < my_age:
12     print("Oops,think bigger!")
13 else:
14     print("think smaller!")
View Code
外層變量,能夠被內層代碼使用
內層變量,不該被外層代碼使用

 

十3、表達式for loop

最簡單的循環10次

1 #_*_coding:utf-8_*_
2 __author__ = 'Alex Li'
3  
4  
5 for i in range(10):
6     print("loop:", i )

輸出:

 1 loop: 0
 2 loop: 1
 3 loop: 2
 4 loop: 3
 5 loop: 4
 6 loop: 5
 7 loop: 6
 8 loop: 7
 9 loop: 8
10 loop: 9

需求一:仍是上面的程序,可是遇到小於5的循環次數就不走了,直接跳入下一次循環

1 for i in range(10):
2     if i<5:
3         continue #不往下走了,直接進入下一次loop
4     print("loop:", i )

需求二:仍是上面的程序,可是遇到大於5的循環次數就不走了,直接退出

1 for i in range(10):
2     if i>5:
3         break #不往下走了,直接跳出整個loop
4     print("loop:", i )

十4、while loop   

 有一種循環叫死循環,一經觸發,就運行個天荒地老、海枯石爛。

海枯石爛代碼

1 count = 0
2 while True:
3     print("你是風兒我是沙,纏纏綿綿到天涯...",count)
4     count +=1

其實除了時間,沒有什麼是永恆的,死loop仍是少寫爲好 

上面的代碼循環100次就退出吧

1 count = 0
2 while True:
3     print("你是風兒我是沙,纏纏綿綿到天涯...",count)
4     count +=1
5     if count == 100:
6         print("去你媽的風和沙,大家這些脫了褲子是人,穿上褲子是鬼的臭男人..")
7         break

回到上面for 循環的例子,如何實現讓用戶不斷的猜年齡,但只給最多3次機會,再猜不對就退出程序。

 1 #!/usr/bin/env python
 2 # -*- coding: utf-8 -*-
 3  
 4  
 5 my_age = 28
 6  
 7 count = 0
 8 while count < 3:
 9     user_input = int(input("input your guess num:"))
10  
11     if user_input == my_age:
12         print("Congratulations, you got it !")
13         break
14     elif user_input < my_age:
15         print("Oops,think bigger!")
16     else:
17         print("think smaller!")
18     count += 1 #每次loop 計數器+1
19 else:
20     print("猜這麼屢次都不對,你個笨蛋.")
相關文章
相關標籤/搜索