تغییر رنگ زمینه کنسول بر اساس انتخاب کاربر

تمرین آسان 456 visibility link download

برنامه‌ای بنویسید که کاربر یکی از رنگ‌های قرمز، آبی، زرد یا سبز را انتخاب کند و رنگ زمینه کنسول را به رنگ انتخابی تغییر دهد. به عنوان مثال، اگر کاربر رنگ «قرمز» را انتخاب کند، پس‌زمینه کنسول به رنگ قرمز تغییر می‌کند.

👨‍💻 7 ساعت قبل کاربر ناشناس این تمرین رو مشاهده کرد

reply 6

import time
import os
start_time = time.time()

class COLOR:
    def __init__(self,color):
        self.color = color

    def color_terminal(self):
        if self.color == "green":
            print(os.system("color a"))
        if self.color == "blue":
            print(os.system("color b"))
        if self.color == "red":
            print(os.system("color c"))
        if self.color == "purple":
            print(os.system("color d"))
        if self.color == "yellow":
            print(os.system("color e"))
        if self.color == "white":
            return os.system("color f")
while True:
    color = str(input("enter your color name\ngreen,blue,red,purple,yellow,white ="))
    print(COLOR(color).color_terminal())

    if time.time() - start_time > 15:
        print("time out")
        break

import os

def set_bg_color(color):
    colors = {
        "قرمز": "41",
        "آبی": "44",
        "زرد": "43",
        "سبز": "42"
    }
    code = colors.get(color)
    if code:
        print(f"\033[{code}m", end="")
    else:
        print("رنگ نامعتبر است.")

color = input("یک رنگ انتخاب کنید (قرمز، آبی، زرد، سبز): ")
set_bg_color(color)
print("رنگ پس‌زمینه تغییر کرد.")
import os

# پاک کردن صفحه کنسول قبل از شروع
os.system('cls' if os.name == 'nt' else 'clear')

# دیکشنری رنگ‌ها (کد ANSI برای پس‌زمینه)
colors = {
    1: ("\033[41m", "Red"),     # قرمز
    2: ("\033[44m", "Blue"),    # آبی
    3: ("\033[43m", "Yellow"),  # زرد
    4: ("\033[42m", "Green")    # سبز
}

# نمایش منوی انتخاب رنگ
print("Choose a background color:")
for num, (_, name) in colors.items():
    print(f"{num}. {name}")

# گرفتن ورودی از کاربر
try:
    choice = int(input("\nEnter your choice (1-4): "))
    if choice in colors:
        color_code, color_name = colors[choice]
        # تغییر رنگ پس‌زمینه‌ی خط چاپ شده
        print(color_code + f"  Background changed to {color_name}!  " + "\033[0m\n")
    else:
        # اگر عدد وارد شده معتبر نبود
        print("Invalid choice! Please run the program again.")
except ValueError:
    # اگر ورودی عدد نبود
    print("Please enter a valid number (1–4).")

import os

print("""Choose one color of them:
          blue, red, green, yellow """)



def change_color():
    x = input("Enter color: ")
    if x == "red":
        os.system('color 4f')
    elif x == "blue":
        os.system('color 1f')
    elif x == "yellow":
        os.system('color 6f')
    elif x == "green":
        os.system('color 2f')
    else:
        print("Enter true color!!!")
        change_color()

change_color()

اینم اولین جواب

fun main() {
while (true) {
println("یک رنگ انتخاب کنید: قرمز، آبی، زرد، سبز (یا 'خروج' برای پایان)")
print("رنگ شما: ")
val color = readLine()?.lowercase() ?: ""

    if (color == "خروج") {
        println("برنامه پایان یافت.")
        break
    }

    val bgColor = when (color) {
        "قرمز" -> "\u001B[41m"
        "سبز" -> "\u001B[42m"
        "زرد" -> "\u001B[43m"
        "آبی" -> "\u001B[44m"
        else -> null 
    }

    if (bgColor != null) {
        println(bgColor + "رنگ پس‌زمینه به $color تغییر کرد!" + "\u001B[0m")
    } else {
        println("رنگ مورد نظر یافت نشد!")
    }

    println()
}

}

reply ارسال جواب

  • قبل از ارسال جواب ویدیو زیر رو ببین تا کار کردن با markdown رو یاد بگیری
  • لطفا جواب های تکراری ارسال نکن
  • در متن جواب اطلاعات شخصی شامل ایمیل و شماره موبایل و آی دی و... ننویس
  • سعی کن داخل کدت از کلمات فارسی یا فینگلیش (فارسی با حروف انگلیسی) استفاده نکنی و کدت تماما انگلیسی باشه
  • لطفا داخل جواب از ایموجی یا کاراکترهای خاص استفاده نکن
  • ارسال جواب حق مادی یا معنوی برای ارسال کننده ایجاد نمیکند و تمام حقوق برای سایت کدبزن محفوظ است

راهنمای ارسال جواب 👇

×
بستن