[英]Working on Python Homework, Having Trouble With Loop
我目前正在大学里学习初学者的Python课程。 我的家庭作业的一部分是创建一个类似于著名的儿童歌曲《 99 Bottles of Pop》的程序。 任务是:
有道理吧? 所以,这是我的代码:
#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时不会发生这种情况吗?
非常感谢您,我非常感谢您的帮助!
看来您有混叠问题。
在循环外部,您要求用户输入数字,例如输入101,然后设置bottleCount = userBottle
。 然后在循环中,将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')
您应该只使用一个变量来跟踪瓶子的数量
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))
这避免了瓶循环。 它改用list-comprehension:
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:
因为这是您在每个步骤中递减的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.