Python上的作业,麻烦与循环
我目前在我的大学初学者的Python课程。我的作业部分是创建一个类似着名儿童歌曲“99瓶流行音乐”的节目。任务是:Python上的作业,麻烦与循环
- 用户输入一个项目。
- 该程序打印出如下陈述:“在墙上弹出x瓶,弹出x瓶,取下一个,通过它,(x-1)瓶在墙上弹出”直到它达到0 一旦达到0,程序必须停下来,并说墙上没有瓶子弹出。
- 上限为99。如果用户输入任何超过99,则显示一个错误并迫使计数器开始在99
有道理正确?所以,这里是我的代码:
#Bottles of Pop on the Wall
#User inputs a number to start with
#Program returns with singing the popular song down to 0
#
print("Bottles of Pop on the Wall Program")
userBottle = int(input("How many bottles? "))
bottleCount = userBottle
while bottleCount > 1:
newBottle = userBottle - 1
bottleCount -= 1
if bottleCount > 99:
print("ALERT: no more than 99 bottles allowed, reverting to 99 bottles")
userBottle = 99
bottleCount = 99
newBottle = 99
print(userBottle , "bottles of pop on the wall, ", userBottle , "bottles of pop" ,
"\ntake one down, pass it around, " , newBottle , "bottles of pop on the wall")
userBottle -= 1
if bottleCount == 1:
print(userBottle , "bottle of pop on the wall, ", userBottle , "bottle of pop" ,
"\ntake one down, pass it around, " , "no bottles of pop on the wall")
input("\nThank you for playing! Press Enter to exit.")
因此,如果用户输入任何低于100的数字,程序将完美工作。但是,如果用户输入超过99的任何东西,那就是我遇到问题的地方。
会发生什么是循环将运行到1,但不会结束。它会重复一次,它会返回:
1 bottles of pop on the wall, 1 bottles of pop
take one down, pass it around, 0 bottles of pop on the wall
0 bottle of pop on the wall, 0 bottle of pop
take one down, pass it around, no bottles of pop on the wall
Thank you for playing! Press Enter to exit.
显然这是不正确的。我的循环出了什么问题,所以我可以确保当用户输入大于99的数字时不会发生这种情况?
非常感谢,我非常感谢您的帮助!
你应该只使用一个变量来跟踪瓶
print("Bottles of Pop on the Wall Program")
bottle_count = int(input("How many bottles? "))
if bottle_count > 99:
print("ALERT: no more than 99 bottles allowed, reverting to 99 bottles")
bottle_count = 99
while bottle_count > 1:
print(bottle_count , "bottles of pop on the wall, ", bottle_count , "bottles of pop")
bottle_count -= 1
print("take one down, pass it around, ", bottle_count, "bottles of pop on the wall")
print(bottle_count , "bottle of pop on the wall, ", bottle_count , "bottle of pop")
print("take one down, pass it around, no bottles of pop on the wall")
input("\nThank you for playing! Press Enter to exit.")
对于下一步,您应该使用的格式字符串和.format
方法的数量。例如。
print("{bottle_count} bottles of pop on the wall, "
"{bottle_count} bottles of pop".format(bottle_count=bottle_count))
这是完美的!我忘记提及练习要求我至少使用一个“while”循环。这不仅解决了错误,而且比我自己的代码更有效率。非常感谢! – Tyler 2014-10-12 18:08:17
看起来你有一个别名问题。
在循环之外,你要求用户输入一个数字,比如说他们输入101,然后你设置bottleCount = userBottle
。然后,在循环中,你的userBottle
值重置为99,但注意以下事项:
In [6]: userBottle = 101
In [7]: bottleCount = userBottle
In [8]: userBottle = 99
In [9]: userBottle
Out[9]: 99
In [10]: bottleCount
Out[10]: 101
以下是为你的程序。您可能已更改了userBottle值,但尚未更改bottleCount值。
要做的事情是编写一个函数来以受控方式获取userBottle值,并且函数只是返回itslef,直到值正确为止。一个例子可能是:
def get_user_bottles(input_message, error_message1, error_message2):
#This makes sure the user inputs a number
try:
userBottle = int(raw_input(input_message))
except ValueError:
print error_message1
return get_user_bottles(input_message, error_message1, error_message2)
#This makes sure the user inputs a number between 1 and 99
if userBottle not in range(1, 100):
print error_message2
return get_user_bottles(input_message, error_message1, error_message2)
else:
return userBottle
userBottle = get_user_bottles('How many bottles?',
'The value must be an integer between 1 and 99',
'That value is out of bounds, it must be between 1 and 99')
这可以避免循环瓶子。它使用列表理解来代替:
n = int(input("How many bottles? "))
if n > 99:
print("Sorry. You cannot afford %s bottles. Starting with 99." % n)
n = 99
song = '\n'.join('%s bottles of pop on the wall, %s bottles of pop\ntake one down, pass it around, %s bottles of pop on the wall' % (i,i,i-1) for i in range(n, 0, -1))
print(song + "\nno bottles of pop on the wall, no bottles of pop\n")
input("Thank you for playing! Press Enter to exit.")
输出示例:
How many bottles? 2
2 bottles of pop on the wall, 2 bottles of pop
take one down, pass it around, 1 bottles of pop on the wall
1 bottles of pop on the wall, 1 bottles of pop
take one down, pass it around, 0 bottles of pop on the wall
no bottles of pop on the wall, no bottles of pop
Thank you for playing! Press Enter to exit.
我把修改歌曲的最后一行的自由。在你有0瓶的流行音乐之后,我不认为有必要从墙上再拿下一个。如果您愿意,可以轻松将其更改回来。
简单的改变: if bottleCount == 1:
这样: if userBottle == 1:
,因为这是你在递减每一步的变量。
当我还是个孩子的时候,这是瓶啤酒...... – 2014-10-12 04:06:50
如果count> 99并且userBottle变为99,那么newBottle是不是98? – csmckelvey 2014-10-12 04:08:36
首先,我将把n> 99的检查移出循环。一旦你检查过了,它再也不会是假的了。我相信这也能修复你的bug – 2014-10-12 04:09:27