python - 不需要的打印重复
问题描述
我正在做一个简单的功能来打印颜色,输入给我单词的第一个字母,但我不知道为什么它会打印很多次。
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!
解决方案
用退货代替您的“印刷品”。您最初的 return 语句再次调用您的函数,该函数在应该返回颜色时打印颜色。
if color_letter.lower()=="r":
return "Red!"
推荐阅读
- javascript - 当用户直接点击需要来自另一个组件的 id 的 url 时,React 中的路由问题
- git - 使用不同的提交消息强制推送提交 - 获取“所有最新信息”通知
- tensorflow - Apache BEAM 管道在写入 TF 记录时失败 - AttributeError: 'str' object has no attribute 'iteritems'
- python - 比较不同、不平等数据集中的日期并为给定日期范围增加价值
- maven - 通过同一个 Jenkins Job 在多个服务器上执行应用程序的部署
- php - 如何使用 PHP 更改文件的 Title 属性
- javascript - AngularJS:将小数分隔符更改为逗号
- java - 存储个人资料图片的图像文件的问题
- c# - 从 YouTube 获取用户的所有播放列表。在 api explorer 中工作,但不在 c# 中
- testing - 我可以通过 cypress api 关闭特定选项卡吗?