正在做Python作业,循环有问题

2024-10-02 14:16:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在在我的大学里学习Python初学者课程。我的家庭作业的一部分是创建一个类似于著名的儿童歌曲99瓶汽水的程序。任务是:

  1. 用户输入一个项目。在
  2. 程序打印出这样一个声明:“墙上有x瓶汽水,x瓶汽水。取下一个,传给四周,(x-1)瓶汽水“直到它达到0。 一旦达到0,程序必须停止并说墙上没有瓶子。在
  3. 上限是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,但不会结束。它将重复最后一次,并返回:

^{pr2}$

显然这是不正确的。我的循环有什么问题,所以我可以确保当用户输入大于99的数字时不会发生这种情况?在

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


Tags: oftheto用户程序onpopdown
3条回答

看起来你有别名问题。在

在循环外,您要求用户输入一个数字,假设他们输入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值,该函数只返回它的lef,直到值正确为止。例如:

^{pr2}$

你应该用一个变量来跟踪瓶子的数量

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方法。例如

^{pr2}$

这样可以避免在瓶子上循环。它使用列表理解来代替:

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

样本输出:

^{pr2}$

我擅自修改了这首歌的最后一行。你喝了0瓶汽水后,我觉得再从墙上拿一瓶是没有意义的。如果你愿意,你可以很容易地把它改回来。在

相关问题 更多 >

    热门问题