【问题标题】:unwanted print repetition不需要的打印重复
【发布时间】:2019-01-10 14:26:15
【问题描述】:

我正在做一个简单的函数来打印颜色,输入给我单词的第一个字母,但我不知道为什么它会打印很多次。

color_letter=input("Please write the first letter of your favorite color: ")

def rainbow_color(color_letter):

    if color_letter.lower()=="r":
        print("Red!")
    elif color_letter.lower()=="o":
         print("That's orange!")
    elif color_letter.lower()=="y":
        print("That most be yellow")
    elif color_letter.lower()=="g":
        print("Green!")
    elif color_letter.lower()=="b":
        print("is it blue?")
    elif color_letter.lower()=="i":
        print("why would you choose indigo wirdo")
    elif color_letter.lower()=="v":
        print("Aww violet")
    else:
        print("wtf are you talking about")

    return  rainbow_color(color_letter)



print(rainbow_color(color_letter))

这就是我得到的(但太多了)

Please write the first letter of your favorite color: r

Red!

Red!

Red!

Red!

【问题讨论】:

  • 仔细查看您的退货声明。

标签: python python-3.x loops if-statement printing


【解决方案1】:

用退货代替您的“印刷品”。您原来的 return 语句再次调用了您的函数,该函数在应该返回颜色时打印颜色。

if color_letter.lower()=="r":
    return "Red!"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多