python 混色程序

vdgimpew  于 2023-09-29  发布在  Python
关注(0)|答案(7)|浏览(94)

提示语:
红色、蓝色和黄色被称为原色,因为它们不能由其他颜色混合而成。当你混合两种原色时,你会得到一种辅助色:当你把红色和蓝色混在一起时,就得到紫色。当你把红色和黄色混在一起时,你就得到了橙子。当你把蓝色和黄色混在一起时,你就得到了绿色。
设计一个程序,提示用户输入两种原色的名称,一次输入一种。如果用户输入的不是“red”、“blue”或“yellow”,程序应该打印“You didn't input two primary colors.”否则,它应该以如下格式打印:
“当你混合红色和蓝色时,你得到紫色。”(假设用户输入了“红色”和“蓝色”。)
我的程序一直得到错误的标准输出
我是这么写的

primary_colora = input("Enter primary color:")
primary_colorb = input("Enter primary color:")
primary_colors = primary_colora or primary_colorb

if primary_colora == (red, blue, yellow):
    primary_colora = True

elif primary_colorb == (red, blue, yellow):
    primary_colorb = True

elif primary_colors == red or blue:
    print("When you mix red and blue, you get purple")

elif primary_colors == yellow or blue:
    print("When you mix yellow and blue, you get green")

elif primary_colors == yellow or red:
    print("When you mix yellow and red, you get orange")

else: print("You didn't input two primary colors.")
7z5jn7bk

7z5jn7bk1#

你需要改变你的语句来匹配字符串而不是变量的颜色-

if primary_colora in ['red', 'blue', 'yellow']:

等等

ghg1uchk

ghg1uchk2#

我环顾四周,发现在语句中合并and & or的方法要简单得多
这里的代码

primary_color1 = input("Enter primary color:")
primary_color2 = input("Enter primary color:")

if (primary_color1 == "red" and primary_color2 == "blue") or (primary_color1 == "blue" and primary_color2 == "red"):
    print("When you mix red and blue, you get purple.")

 elif (primary_color1 == "blue" and primary_color2 == "yellow") or (primary_color1 == "yellow" and primary_color2 == "blue"):
    print("When you mix blue and yellow, you get green.")

elif (primary_color1 == "yellow" and primary_color2 == "red") or (primary_color1 == "red" and primary_color2 == "yellow"):
    print("When you mix yellow and red, you get orange.")

else: print("You didn't input two primary colors.")
0qx6xfy6

0qx6xfy63#

代码几乎是正确的,我添加了一些东西,使其工作

primary_colora = input("Enter primary color:")
primary_colorb = input("Enter primary color:")
primary_colors = primary_colora or primary_colorb

if primary_colora == ['red', 'blue', 'yellow']:
primary_colora = True

elif primary_colorb == ['red', 'blue', 'yellow']:
primary_colorb = True

elif primary_colors == 'red' or 'blue':
print("When you mix red and blue, you get purple")

elif primary_colors == 'yellow' or 'blue':
print("When you mix yellow and blue, you get green")

elif primary_colors == 'yellow' or 'red':
print("When you mix yellow and red, you get orange")

else: print("You didn't input two primary colors.")
zzzyeukh

zzzyeukh4#

这是我的变种。如果有可能简化代码,请给予我一个提示。

color1=str(input('Enter first primary color: '))
color2=str(input('Enter second primary color: '))

RED='red'
BLUE='blue'
YELLOW='yellow'

if color1 != BLUE and color1 != RED and color1 != YELLOW:
   print('Error')
elif color2 != BLUE and color2 != RED and color2 != YELLOW :
   print('Error')
elif color1 == BLUE and color2 == BLUE:
   print('Error')
elif color1 == YELLOW and color2 == YELLOW:
   print('Error')
elif color1 == RED and color2 == RED:
   print('Error')
elif (color1 == RED and color2 == BLUE) or (color1 == BLUE and color2 == RED):
   print('Purple')
elif (color1 == RED and color2 == YELLOW) or (color1 == YELLOW and color2 == RED):
   print('Orange')
else:
   print('Green')
lp0sw83n

lp0sw83n5#

primary1 = input("Enter primary color:")
primary2 = input("Enter primary color:")

if (primary1.lower() == "red" and primary2.lower() == "blue") or (primary1.lower() == "blue" and primary2.lower() == "red"):
    secondary = "purple"
    print(f"When you mix {primary1.lower()} and {primary2.lower()}, you get {secondary}.")
elif (primary1.lower() == "red" and primary2.lower() == "yellow") or (primary1.lower() == "yellow" and primary2.lower() == "red"):
    secondary = "orange"
    print(f"When you mix {primary1.lower()} and {primary2.lower()}, you get {secondary}.")
elif (primary1.lower() == "blue" and primary2.lower() == "yellow") or (primary1.lower() == "yellow" and primary2.lower() == "blue"):
    secondary = "green"
    print(f"When you mix {primary1.lower()} and {primary2.lower()}, you get {secondary}.")
else:
    print("You didn't input two primary colors.")
pgx2nnw8

pgx2nnw86#

while True:
    color1 = input("Enter the first primary color (red, blue, or yellow): ").lower()
    color2 = input("Enter the second primary color (red, blue, or yellow):").lower()

    if color1 == color2:
        print("You can't choose the same primary colors in both variables.")
        break
    elif color1 not in ["red", "blue", "yellow"] or color2 not in ["red", "blue", "yellow"]:
        print("Error: Please enter valid primary colors.")
    else:
        # Determine the secondary color
        if color1 == "red" and color2 == "blue" or color1 == "blue" and color2 == "red":
            secondary_color = "purple"
        elif color1 == "red" and color2 == "yellow" or color1 == "yellow" and color2 == "red":
            secondary_color = "orange"
        elif color1 == "blue" and color2 == "yellow" or color1 == "yellow" and color2 == "blue":
            secondary_color = "green"

        # Display the resulting secondary color
        print(f"The secondary color is {secondary_color}.")
dbf7pr2w

dbf7pr2w7#

在PyCharm中截图。

所以我才能把它做对。

primary1 = input("Enter primary color:")
primary2 = input("Enter primary color:")

if (primary1.lower() == "red" and primary2.lower() == "blue") or (primary1.lower() == "blue" and                                                                
 primary2.lower() == "red"):
   secondary = "purple"
   print(f"When you mix {primary1.lower()} and {primary2.lower()}, you get {secondary}.")
elif (primary1.lower() == "red" and primary2.lower() == "yellow") or (primary1.lower() == "yellow" and
                                                                      primary2.lower() == "red"):
    secondary = "orange"
    print(f"When you mix {primary1.lower()} and {primary2.lower()}, you get {secondary}.")
elif (primary1.lower() == "blue" and primary2.lower() == "yellow") or (primary1.lower() == "yellow" and
                                                                       primary2.lower() == "blue"):
    secondary = "green"
    print(f"When you mix {primary1.lower()} and {primary2.lower()}, you get {secondary}.")
else:
    print("You didn't input two primary colors.")

相关问题