2 回答

TA貢獻1794條經(jīng)驗 獲得超7個贊
您的代碼的問題在于,電梯的下部僅在顯示燈光的 for 循環(huán)結(jié)束后才繪制。您必須在 for 循環(huán)內(nèi)繪制整個電梯,這樣電梯就會在燈光更新時顯示。注意:為了很好地做到這一點,您應(yīng)該在 for 循環(huán)的每次迭代后始終清除控制臺。要執(zhí)行此操作,您必須先執(zhí)行此操作from os import system,然后再清除屏幕,就像system("clear")在 Linux 或 Mac 和system("cls")Windows 上執(zhí)行的操作一樣。這是編輯后的代碼:
from termcolor import cprint
import time
from os import system
level = int(input('Which floor would you like to visit?\n')) + 1
light = '?'
lights = light
for currentlevel in range(1, level):
system('clear')
cprint(' ▲ ▼ ', 'yellow')
cprint(' ┏' + ('━' * 13) + '┓')
print(' ┃', end='')
cprint(lights, 'yellow', end='', flush=True)
print('┃')
print(' ┣━━━━━━╥━━━━━━┫')
print(' ┃ ║ ┃\n' * 5 + ' ┃ ║ ┃')
print('━━━━┗━━━━━━╨━━━━━━┛━━━━')
print(f'\nYou have arrived at floor ', end='')
cprint(currentlevel, 'yellow')
time.sleep(0.5)
lights += light
干杯!
添加回答
舉報