简体   繁体   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")

I am trying to make it so that the user can enter points for any of the four categories, but has no more than 30 points to expend(I have not yet written the code for the health, wisdom or dexterity points). 我试图做到这一点,以便用户可以输入四个类别中任何一个类别的积分,但是最多可以使用30积分(我尚未编写有关健康,智慧或敏捷积分的代码)。 Why when i run the program does the loop only run again if you choose to add a number of points between 1-30? 为什么当我运行程序时,如果您选择在1-30之间添加多个点,则循环只会再次运行? If the user enters the points they want to allocate towards strengthPoints using numbers not between 1-30, the loop will run the associated if statement, but will not run through the loop again, why is this the case? 如果用户使用不在1到30之间的数字输入要分配给strengthPoints的点,则循环将运行关联的if语句,但不会再次循环运行,为什么会这样呢?

you are using the same variable for two different purposes pointsToAdd . 您将相同的变量用于两个不同的目的pointsToAdd You have it as the total points to assign, and what was selected by the user to add to a stat. 您可以将其作为要分配的总分,以及用户选择要添加到统计中的内容。 Once you stomp the total points to assign with the users choice, you then add it to the 0 strength and subtract it from your user entered value, setting it to zero. 一旦用用户的选择踩了总分,就将其加到0强度,然后从用户输入的值中减去它,将其设置为零。 Using to separate variables like below will fix it. 使用如下所示的变量来修复它。

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

You do 你做

pointsToAdd = 30

Then in the loop 然后在循环中

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

Then in the test 然后在测试

# 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

This results in pointsToAdd == 0 after than. 导致比之后的pointsToAdd == 0

You need to use another variable for the input of your user. 您需要使用另一个变量来输入用户。

As other pointed out you are overwriting the same variable pointsToAdd . 正如其他指出的那样,您正在覆盖相同的变量pointsToAdd I also consider that you can cut down your conditions to two: 我还认为您可以将条件减少到两个:

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

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

If you want compatibility between Python 2.x and 3.x, you can use: 如果要在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