使用Python家庭作业,遇到循环问题

时间:2014-10-12 04:05:08

标签: python

我目前正在大学读初学者的Python课程。我的家庭作业的一部分是创建一个类似于着名儿童的歌曲99瓶流行音乐的程序。任务是:

  1. 用户输入项目。
  2. 该程序打印出如下声明:" x瓶墙上的流行音乐,x瓶流行音乐。拿下一个,将它传递,墙上放着(x-1)瓶爆炸物"直到它达到0。 一旦达到0,程序必须停下来,并说墙上没有流行的瓶子。
  3. 限制为99.如果用户输入99以上的任何内容,则显示错误并强制计数器从99开始。
  4. 有道理吗?所以,这是我的代码:

    #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的数字时不会发生这种情况?

    非常感谢你,非常感谢你的帮助!

3 个答案:

答案 0 :(得分:1)

看起来你有一个别名问题。

在循环外,您要求用户输入一个数字,例如输入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值,该函数只返回其自身,直到值正确为止。一个例子可能是:

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')

答案 1 :(得分:0)

您应该只使用一个变量来追踪瓶数

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))

答案 2 :(得分:0)

这可以避免循环瓶子。它使用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瓶流行音乐之后,我认为尝试从墙上取下另一个是不合理的。如果您愿意,可以轻松更改它。