繁体   English   中英

python中的while循环不起作用

[英]While loop in python not working

pointsToAdd = 30
strengthPoints = 0
healthPoints = 0
wisdomPoints= 0
dexterityPoints = 0

while pointsToAdd > 0:
    choice = int(input("Choice(1-4): "))
    if choice == 1:
        pointsToAdd = int(input("How many Strength points would you like to add: "))
        if pointsToAdd < 31 and pointsToAdd > 0 and pointsToAdd - strengthPoints > 0:
            strengthPoints += pointsToAdd
            pointsToAdd -= strengthPoints
            print("You now have",strengthPoints,"strength points")
        elif pointsToAdd > 30:
            print("You cannot add that many!")
        elif pointsToAdd<1:
            print("You cannot add less than one point!")
        elif pointsToAdd - strengthPoints <= 0:
            print("You only have",pointsToAdd,"points!")
        else:
            print("We are sorry, but an error has occurred")

我试图做到这一点,以便用户可以输入四个类别中任何一个类别的积分,但是最多可以使用30积分(我尚未编写有关健康,智慧或敏捷积分的代码)。 为什么当我运行程序时,如果您选择在1-30之间添加多个点,则循环只会再次运行? 如果用户使用不在1到30之间的数字输入要分配给strengthPoints的点,则循环将运行关联的if语句,但不会再次循环运行,为什么会这样呢?

您将相同的变量用于两个不同的目的pointsToAdd 您可以将其作为要分配的总分,以及用户选择要添加到统计中的内容。 一旦用用户的选择踩了总分,就将其加到0强度,然后从用户输入的值中减去它,将其设置为零。 使用如下所示的变量来修复它。

totalPointsToAdd = 30
strengthPoints = 0
healthPoints = 0
wisdomPoints= 0
dexterityPoints = 0

while totalPointsToAdd > 0:
    choice = int(input("Choice(1-4): "))
    if choice == 1:
        pointsToAdd = int(input("How many Strength points would you like to add: "))
    if pointsToAdd < 31 and pointsToAdd > 0 and pointsToAdd - strengthPoints > 0:
        strengthPoints += pointsToAdd
        totalPointsToAdd -= pointsToAdd
        print("You now have",strengthPoints,"strength points")

你做

pointsToAdd = 30

然后在循环中

pointsToAdd = int(input("How many Strength points would you like to add: "))

然后在测试

# Here pointsToAdd is not 30, but the value inputed by the user
strengthPoints += pointsToAdd
# Here strengthPoints == pointsToAdd == the value from the input
pointsToAdd -= strengthPoints
# Here pointsToAdd == 0

导致比之后的pointsToAdd == 0

您需要使用另一个变量来输入用户。

正如其他指出的那样,您正在覆盖相同的变量pointsToAdd 我还认为您可以将条件减少到两个:

pointsToAdd = 30
strengthPoints = 0

while pointsToAdd:
    print ('You have', pointsToAdd, 'available points.')
    choice = int(input("Choice(1-4): "))
    if choice == 1:
        toBeAdded = int(input("How many Strength points would you like to add: "))
        if toBeAdded < 1: # Can't assign negative or null values
            print("You cannot add less than one point!")
            continue
        if toBeAdded > pointsToAdd: # Don't have enough points to spend
            print("You only have", pointsToAdd, "available points!")
            continue
        strengthPoints += toBeAdded
        pointsToAdd -= toBeAdded
        print("You now have", strengthPoints, "strength points")

在Python 2.x中使用:raw_input在Python 3.x中使用:输入

如果要在Python 2.x和3.x之间兼容,可以使用:

try:
    input = raw_input
except NameError:
    pass

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM