• 1400/02/19

بیرون کشیدن float از لیست :

با عرض سلام و وقت بخیر

توی این قطعه کد من وقتی number رو وارد میکنم انتظار دارم که هم اعداد صحیح و هم اعداد اعشاری رو تو خروجی بهم نشون بده اما فقط اعداد صحیح رو بهم نشون میده!

و زمانی هم که به جای and از or استفاده میکنم(در خط دهم) رشته هارو هم بهم نشون میده!

my_list = ["Blue", "Red", "Green", "Yellow", "Gray", 81, 3.1, 2.5, 1, 0]

my_input = input("Write color or number => \n")

if my_input == "color":
    for color in my_list:
        if type(color) == str:
            print(f"Your Color Is : {color}")

elif my_input == "number":
    for number in my_list:
        if type(number) == int and float:
            print(f"Your Number Is: {number}")

else:
    print("Something Wrong...")
  • 1400/02/21
  • ساعت 01:08

سلام 

از این کد استفاده کنید.

my_list = ["Blue", "Red", "Green", "Yellow", "Gray", 81, 3.1, 2.5, 1, 0]

my_input = input("Write color or number => \n")

if my_input == "color":
    for color in my_list:
        if type(color) == str:
            print(f"Your Color Is : {color}")

elif my_input == "number":
    for number in my_list:
         if type(number) == int:
            print(f"Your Number Is: {number}")
         elif type(number) == float:
            print(f"Your Number Is: {number}")

else:
    print("Something Wrong...")

logo-samandehi