python - 我为99瓶歌曲制作了一个程序,但它没有正确打印歌曲
问题描述
def printLyrics(beer):
print(str(beer) + " bottles of beer on the wall, " + str(beer) + " bottles of beer")
print("Take one down and pass it around " + str(beer-1) + " bottles of beer on the wall.")
print()
if beer == 2:
print("2 bottles of beer on the wall, 2 bottles of beer.")
print("Take one down and pass it around, 1 bottle of beer on the wall.")
print()
elif beer == 1:
print("1 bottle of beer on the wall, 1 bottle of beer.")
print("Take one down and pass it around, no more bottles of beer on the wall.")
print()
每次墙上的瓶子运行时都会打印这条线,我不知道如何修复它。
else: print("墙上没有啤酒瓶了,没有啤酒瓶了。") print("去商店再买点,墙上有99瓶啤酒。") print()
def main(): 范围内的啤酒 (99,0,-1): printLyrics(beer)
主要的()
解决方案
第一组打印应以 beer > 2 为条件。
def printLyrics(beer):
if beer>2:
print(str(beer) + " bottles of beer on the wall, " + str(beer) + " bottles of beer")
print("Take one down and pass it around " + str(beer-1) + " bottles of beer on the wall.")
print()
elif beer == 2:
print("2 bottles of beer on the wall, 2 bottles of beer.")
print("Take one down and pass it around, 1 bottle of beer on the wall.")
print()
else:
print("1 bottle of beer on the wall, 1 bottle of beer.")
print("Take one down and pass it around, no more bottles of beer on the wall.")
print()
for beer in range(99,0,-1):
printLyrics(beer)
推荐阅读
- reactjs - 如何从钩子中使用 .then 模拟函数
- python - Python:numpy行的一般总和
- json - 解析 JSON 时出错:输入中有多个文档(Redshift 到 Snowflake SQL)
- azure-data-factory - 如何创建 Azure 数据工厂直到检查 API 是否返回状态值的活动?
- ios - SwiftUI 视图混合了 UIViewController 的视图
- python - 将 Pandas 系列分成几个字符系列
- python - 删除数据框中的空标记
- docker - 为什么 GitHub 操作无法推送 docker 镜像?
- bash - 用于运行程序 100 次并获取结果的脚本
- azure-machine-learning-studio - Azure AutoML 似乎添加了额外的输入?