最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python通過類的組合模擬街道紅綠燈

 更新時間:2020年09月16日 15:10:34   作者:秋天中的一片葉  
這篇文章主要介紹了Python通過類的組合模擬街道紅綠燈,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

一,紅綠燈揭示板案例思路

1. 創(chuàng)建Traffic_light紅綠燈類

(1)靜態(tài)屬性 :

<1> 綠燈時間,<2> 黃燈時間 , <3> 紅燈時間, <4> 兩塊顯示時間的電子屏

(2)動態(tài)屬性

<1> 輸入紅黃綠時間函數(shù)(靜態(tài)函數(shù)),<2> 紅黃綠時間倒計(jì)時函數(shù) ,
<3> 構(gòu)造電子屏數(shù)字的顯示函數(shù),<4> 顯示兩塊電子屏綁定兩位數(shù)的顯示函數(shù)
<5> 實(shí)例化對象展示電子屏函數(shù)

2. 電子屏類的創(chuàng)建(Light):

python中沒有數(shù)組,因此自己創(chuàng)建函數(shù)把獲取到的值存放到數(shù)組中

(存放內(nèi)容: 20行,10列的布爾值)

3. input_time(color:str)函數(shù)的創(chuàng)建

<1> 導(dǎo)入colorama包并初始化實(shí)現(xiàn)windows命令行下顏色字體打印效果
<2> 輸入紅黃綠時間的字體成對應(yīng)的顏色
<3> 通過colorama類方法實(shí)現(xiàn)輸入的紅黃綠時間為對應(yīng)的顏色展示
<4> 對輸入的數(shù)字進(jìn)行校驗(yàn)(必須為1-99之間的正數(shù)。因?yàn)橐粔K電子屏只記錄一位數(shù)字)
<5> 返回相應(yīng)的值

4. Countdown數(shù)字倒計(jì)時函數(shù)的創(chuàng)建

<1> 通過while循環(huán)讓三個燈的狀態(tài)一直循環(huán)持續(xù)
<2> 對于紅黃綠燈輸入的數(shù)字進(jìn)行遞減打印流程如下
#流程: 清屏-->打印完后 -->暫停1秒鐘-->清屏 -->數(shù)字減一后再打印-->再暫停1秒鐘-->清屏-->再數(shù)字減一打印
<3> 導(dǎo)入time,os,colorama等需要的包

5.build_LED_number函數(shù)的創(chuàng)建

之前創(chuàng)建的電子屏是默認(rèn)False的狀態(tài)。分別構(gòu)造0-9的狀態(tài)在電子屏中True的狀態(tài)的顯示

6.print_LED函數(shù)的創(chuàng)建

兩塊電子屏,分別顯示輸入時間的第一位和第二位數(shù)字.如果數(shù)字為單數(shù)則前面用零補(bǔ)齊的方法顯示。兩塊屏并排顯示每一位數(shù)字,從而展示電子版的效果

7.注意事項(xiàng):

因?yàn)槲覀冇玫搅薿s,及colorama類。所以最終效果的展示不是在pycharm中展示。而是在windows的cmd命令行中展示。

原因是因?yàn)槲覀兇a中調(diào)用了os.system("cls")這個清屏命令。在pycharm中是很難做到清屏的效果。

另外在pycharm中對于電子屏的展示效果也不如windows cmd中展示的效果俱佳。因此運(yùn)行程序是請?jiān)趙indows命令行中運(yùn)行。

二,紅綠燈揭示板代碼的呈現(xiàn)

import time
import os
from colorama import init,Fore,Back,Style
#命令行模式字體顏色初始化
init(autoreset=True)

#電子屏類
class Light:
  #構(gòu)造函數(shù)
  def __init__(self):
    self.light = [] #存儲行列數(shù)組的集合

    #自動初始化
    self.prepare_light()

  def prepare_light(self):
    """
    電子屏的創(chuàng)建
    python中沒有數(shù)組.因此通過類,函數(shù)來創(chuàng)建數(shù)組得到一個20行10列的數(shù)組
    :return:
    """
    for row in range(20): #20行
      temp = [] # 臨時存儲每行10個圈
      for col in range(10): #10列
        temp.append(False) #默認(rèn)燈都是不亮的因此通過布爾類型的False表示不亮的狀態(tài)
      #把行列排的200個燈的狀態(tài)存入到light集合中
      self.light.append(temp)

#紅綠燈類
class Traffic_light:
  #構(gòu)造函數(shù),靜態(tài)屬性
  def __init__(self,green_time,yellow_time,rea_time):
    self.green_time = green_time #綠燈時間
    self.yellow_time = yellow_time #黃燈時間
    self.red_time = rea_time #紅燈時間

    #通過類的組合調(diào)用Light類函數(shù)
    self.number01 = Light() #創(chuàng)建第一個電子屏
    self.number02 = Light() #創(chuàng)建第二個電子屏

  #紅黃綠等時間倒計(jì)時函數(shù)
  def countdown(self):
    while True:
      #流程: 清屏-->打印完后 -->暫停1秒鐘-->清屏 -->數(shù)字減一后再打印-->再暫停1秒鐘-->清屏-->再數(shù)字減一打印
      for number in range(self.green_time,-1,-1):
        #第一個-1代表取值到0,如果設(shè)置0則取值取不到0.第二個-1代表數(shù)字減一
        os.system("cls") #清屏
        self.start_display(number,"green") #調(diào)用start_display函數(shù)傳數(shù)字及顏色
        time.sleep(1) #停止一秒鐘

    # 黃燈倒計(jì)時
      for number in range(self.yellow_time,-1,-1):
        os.system("cls") #清屏
        self.start_display(number,"yellow")
        time.sleep(1) #停止一秒鐘


    # 紅燈倒計(jì)時
      for number in range(self.red_time,-1,-1):#第一個-1代表取值到0,如果設(shè)置0則取值取不到0.第二個-1代表數(shù)字減一
        os.system("cls") #清屏
        self.start_display(number,"red")
        time.sleep(1) #停止一秒鐘

  @staticmethod  #靜態(tài)方法不需要初始化
  def input_time(color:str):
    # 設(shè)置全局變量(便于靜態(tài)方法使用)
    time = ""
    while True:
      if color.lower() in ["green","綠色","綠","綠燈"]:
        print(Fore.GREEN + "請輸入綠燈的時間:",end="") #實(shí)現(xiàn)打印字體呈現(xiàn)顏色效果
        time = input()
      if color.lower() in ["yellow", "黃色", "黃", "黃燈"]:
        print(Fore.YELLOW + "請輸入黃燈的時間:", end="")
        time = input()
      if color.lower() in ["red", "紅色", "紅", "紅燈"]:
        print(Fore.RED + "請輸入紅燈的時間:", end="")
        time = input()

      #校驗(yàn)輸入的是否合規(guī)
      if not time.isdigit():
        print("輸入的值不符合要求?!疽?必須是1-99之間的正數(shù)?!?)
        continue
      else:
        time_number = int(time) # 因?yàn)閠ime是字符串.拿到數(shù)字后轉(zhuǎn)成Int類型再判斷
        if time_number < 1 or time_number > 99:
          print("輸入的值不符合要求?!疽?必須是1-99之間的正數(shù)?!?)
          continue
        else:
          return time_number

  def build_LED_number(self,char:str):
    """
    :param char: LED燈數(shù)字的構(gòu)造
    :return: 返回temp_LED這個數(shù)組
    """
    temp_LED = Light() #臨時創(chuàng)建新的數(shù)組

    if char == "0": #構(gòu)造0
      for row in range(20):
        for col in range(10):
          if row < 2: #最上面兩列
            temp_LED.light[row][col] = True
          if row > 17: #最下面兩列
            temp_LED.light[row][col] = True
          if col < 2:#最左邊兩列
            temp_LED.light[row][col] = True
          if col > 7: #最后面兩列
            temp_LED.light[row][col] = True

    elif char == "1": #構(gòu)造1
      for row in range(20):
        for col in range(10):
          if col > 7: #最后面兩列
            temp_LED.light[row][col] = True

    elif char == "2": #構(gòu)造2
      for row in range(20):
        for col in range(10):
          if row < 2: # 最上面兩列
            temp_LED.light[row][col] = True
          if col > 7 and row < 9: # 最后面兩列
            temp_LED.light[row][col] = True
          if row == 9 or row == 10: # 中間兩行
            temp_LED.light[row][col] = True

          if col < 2 and row >10: #左邊列
            temp_LED.light[row][col] = True
          if row > 17: # 最下面兩列
            temp_LED.light[row][col] = True
    elif char == "3": #構(gòu)造3
      for row in range(20):
        for col in range(10):
          if row < 2: # 最上面兩列
            temp_LED.light[row][col] = True
          if col > 7 : # 最后面兩列
            temp_LED.light[row][col] = True
          if row == 9 or row == 10: # 中間兩行
            temp_LED.light[row][col] = True
          if row > 17: # 最下面兩列
            temp_LED.light[row][col] = True
    elif char == "4": # 構(gòu)造4
      for row in range(20):
        for col in range(10):
          if col < 2 and row <9: # 最上面兩列
            temp_LED.light[row][col] = True
          if col > 7: # 最后面兩列
            temp_LED.light[row][col] = True
          if row == 9 or row == 10: # 中間兩行
            temp_LED.light[row][col] = True
    elif char == "5": # 構(gòu)造5
      for row in range(20):
        for col in range(10):
          if row < 2:
            temp_LED.light[row][col] = True
          if col < 2 and row < 9:
            temp_LED.light[row][col] = True
          if row == 9 or row == 10:
            temp_LED.light[row][col] = True
          if col > 7 and row > 10:
            temp_LED.light[row][col] = True
          if row > 17:
            temp_LED.light[row][col] = True
    elif char == "6": # 構(gòu)造6
      for row in range(20):
        for col in range(10):
          if row < 2:
            temp_LED.light[row][col] = True
          if col < 2:
            temp_LED.light[row][col] = True
          if row == 9 or row == 10:
            temp_LED.light[row][col] = True
          if col > 7 and row > 10:
            temp_LED.light[row][col] = True
          if row > 17:
            temp_LED.light[row][col] = True
    elif char == "7": # 構(gòu)造7
      for row in range(20):
        for col in range(10):
          if row < 2:
            temp_LED.light[row][col] = True
          if col > 7:
            temp_LED.light[row][col] = True


    elif char == "8": #構(gòu)造8
      for row in range(20):
        for col in range(10):
          if row < 2: #最上面兩列
            temp_LED.light[row][col] = True
          if row > 17: #最下面兩列
            temp_LED.light[row][col] = True
          if row == 9 or row == 10: # 中間兩行
            temp_LED.light[row][col] = True
          if col < 2:#最左邊兩列
            temp_LED.light[row][col] = True
          if col > 7: #最后面兩列
            temp_LED.light[row][col] = True

    elif char == "9": # 構(gòu)造9
      for row in range(20):
        for col in range(10):
          if row < 2: # 最上面兩列
            temp_LED.light[row][col] = True
          if col < 2 and row < 9:
            temp_LED.light[row][col] = True
          if row > 17: # 最下面兩列
            temp_LED.light[row][col] = True
          if row == 9 or row == 10: # 中間兩行
            temp_LED.light[row][col] = True
          if col > 7: # 最后面兩列
            temp_LED.light[row][col] = True

    #返回值
    return temp_LED

  def print_LED(self,color:str):
    for row in range(20):
      #打印第一個數(shù)
      for col01 in range(10):
        if self.number01.light[row][col01] == True:
          if color == "green":
            print(Fore.GREEN + "●",end="")
          elif color == "yellow":
            print(Fore.YELLOW + "●",end="")
          elif color == "red":
            print(Fore.RED + "●",end="")
        else:
          print(" ",end="") # 兩個全角空格 注釋:○占用的字符相當(dāng)于兩個全角空格的占位
      print("\t",end="")
      #打印第二個數(shù)
      for col02 in range(10):
        if self.number02.light[row][col02] == True:
          if color == "green":
            print(Fore.GREEN + "●",end="")
          elif color == "yellow":
            print(Fore.YELLOW + "●",end="")
          elif color == "red":
            print(Fore.RED + "●",end="")
        else:
          print(" ",end="")
      #換行
      print()

  def start_display(self,number:int,color:str):
    """
    電子屏展示
    :param number:電子屏上展示的數(shù)字
    :param color: 電子屏上展示的顏色
    :return:
    """
    number_str = "%02d" % number #傳進(jìn)來的數(shù)字2位顯示
    self.number01 = self.build_LED_number(number_str[0]) #把數(shù)字的第一位給第一個電子屏
    self.number02 = self.build_LED_number(number_str[1]) #把數(shù)字的第二位給第二個電子屏

    #在電子屏上顯示
    self.print_LED(color)

if __name__ == "__main__":
  green_time = Traffic_light.input_time("綠燈")
  yellow_time = Traffic_light.input_time("黃燈")
  red_time = Traffic_light.input_time("紅燈")

  #實(shí)例化
  traffic01 = Traffic_light(green_time,yellow_time,red_time)
  traffic01.countdown()

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python實(shí)現(xiàn)發(fā)送email的幾種常用方法

    Python實(shí)現(xiàn)發(fā)送email的幾種常用方法

    這篇文章主要介紹了Python實(shí)現(xiàn)發(fā)送email的幾種常用方法,非常實(shí)用,需要的朋友可以參考下
    2014-08-08
  • Python制作可視化報表的示例詳解

    Python制作可視化報表的示例詳解

    在數(shù)據(jù)展示中使用圖表來分享自己的見解,是個非常常見的方法。這也是Tableau、Power BI這類商業(yè)智能儀表盤持續(xù)流行的原因之一。本文主主要介紹了一個用Python制作可視化報表的案例,感興趣的可以學(xué)習(xí)一下
    2022-02-02
  • Python實(shí)現(xiàn)蒙特卡洛模擬的示例代碼

    Python實(shí)現(xiàn)蒙特卡洛模擬的示例代碼

    這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)蒙特卡洛模擬,文中的示例代碼講解詳細(xì),具有一定的參考價值,感興趣的小伙伴可以了解一下
    2023-03-03
  • Python爬蟲中Selenium實(shí)現(xiàn)文件上傳

    Python爬蟲中Selenium實(shí)現(xiàn)文件上傳

    這篇文章主要介紹了Python爬蟲中Selenium實(shí)現(xiàn)文件上傳,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 基于OpenCV實(shí)現(xiàn)小型的圖像數(shù)據(jù)庫檢索功能

    基于OpenCV實(shí)現(xiàn)小型的圖像數(shù)據(jù)庫檢索功能

    下面就使用VLAD表示圖像,實(shí)現(xiàn)一個小型的圖像數(shù)據(jù)庫的檢索程序。下面實(shí)現(xiàn)需要的功能模塊,分步驟給大家介紹的非常詳細(xì),對OpenCV圖像數(shù)據(jù)庫檢索功能感興趣的朋友跟隨小編一起看看吧
    2021-12-12
  • 使用python實(shí)現(xiàn)excel的Vlookup功能

    使用python實(shí)現(xiàn)excel的Vlookup功能

    這篇文章主要介紹了使用python實(shí)現(xiàn)excel的Vlookup功能,當(dāng)我們想要查找的數(shù)據(jù)量較大時,這時則有請我們的主角VLookup函數(shù)出場,那么如何用python實(shí)現(xiàn)VLookup呢,需要的朋友可以參考下
    2023-04-04
  • Python?Fire實(shí)現(xiàn)自動生成命令行接口

    Python?Fire實(shí)現(xiàn)自動生成命令行接口

    命令行程序是平時寫一些小工具時最常用的方式,隨著命令行程序功能的豐富,也就是參數(shù)多了以后,解析和管理參數(shù)之間的關(guān)系會變得越來越繁重,而本次介紹的?Fire?庫正好可以解決這個問題,下面我們就來看看具體實(shí)現(xiàn)方法吧
    2023-09-09
  • Python線程之同步機(jī)制實(shí)際應(yīng)用場景舉例說明

    Python線程之同步機(jī)制實(shí)際應(yīng)用場景舉例說明

    這篇文章主要給大家分享的是Python線程之同步機(jī)制實(shí)際應(yīng)用場景舉例說明,銀行轉(zhuǎn)賬小栗子供大家參考學(xué)習(xí),希望對你有一定的幫助
    2022-02-02
  • pycharm 更改創(chuàng)建文件默認(rèn)路徑的操作

    pycharm 更改創(chuàng)建文件默認(rèn)路徑的操作

    今天小編就為大家分享一篇pycharm 更改創(chuàng)建文件默認(rèn)路徑的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python 實(shí)現(xiàn)反轉(zhuǎn)整數(shù)的案例(很容易懂的那種)

    Python 實(shí)現(xiàn)反轉(zhuǎn)整數(shù)的案例(很容易懂的那種)

    這篇文章主要介紹了Python 實(shí)現(xiàn)反轉(zhuǎn)整數(shù)的案例(很容易懂的那種),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03

最新評論

读书| 金沙县| 安龙县| 射阳县| 淮阳县| 会宁县| 武城县| 玉林市| 广州市| 新建县| 南江县| 洛阳市| 凤城市| 汉中市| 永善县| 桑植县| 东丰县| 托里县| 唐河县| 株洲市| 霍林郭勒市| 大荔县| 济源市| 新昌县| 红河县| 磐安县| 兴仁县| 木兰县| 麻阳| 元阳县| 如皋市| 施甸县| 邵武市| 长春市| 田东县| 和平区| 永嘉县| 罗定市| 改则县| 沧州市| 肇东市|