Python for Absolute Beginner包版广告;第5章,挑战#2 - 语法无效

时间:2017-05-05 02:26:42

标签: python python-3.x while-loop syntax-error

我是编程新手;我大约一个月前才开始认真研究它。

我读过的书是绝对初学者的Python,我在第5章的挑战部分遇到了障碍。我一直在以稳定的速度完成本书,并且以前只有一次挑战失败。这个新的给我一个"无效的语法"错误,我不知道为什么。

对我来说(诚然没有经验),一切似乎都井然有序; 我无法理解我做错了什么。

以下是代码:

 #Character Creator

#Establish defualt stats
ws = 10
st = 10
dx = 10
ht = 10
points = 30

#Tell the user about the program
print(
    """
Welcome to the character creator. Here you will allocate a pool of 30
points between 4 different stats: Strength, Dexterity, Health, and Wisdom.
You may also decrease any of these stats to gain extra points.
The game is over when your point pool is empty.
    """)

#Set the selection process to continue until the user has spent their points
while points > 0:
    #Tell the user what their starting stats are and how to select them
    print("\nHere are your current stats: ")
    print("1 - Strength: ", st)
    print("2 - Dexterity: ", dx)
    print("3 - Health: ", ht)
    print("4 - Wisdom : ", ws)
    print("Remaining points: ", points)

    #Let the player choose a stat to interact with
    chosen_stat = input("\nSelect a stat: ")
    #What to do if the player chooses Strength
    if chosen_stat == "1":
        print("\nYou have selected Strength.")
        print("Press 1 to increase it")
        print("Press 2 to decrease it")
        #Establish if the stat is being increased or decreased
        up_or_down_st = input("")
        #If the stat is being increased
        if up_or_down_st == "1":
            spent_points = int(input("How many points will you spend?: ")
            #Ensure that the player cannot spend more points than they have; 'while' this is where the error is
            while spent_points > points:
                               print("You don't have that many points.")
                               spent_points = int(input("Try a lower number: ")

            #Allocate the points and inform the user
            st += spent_points
            points -= spent_points
            print("Your have spent ", spent_points, " and increased your Strength to ", st)

        #The process is almost identical for decreasing stats
        if up_or_down_st == "2":
            gained_points = int(input("How many points will you deduct?: ")
            while gained_points > st:
                               print("You don't have that many points.")
                               spent_points = int(input("Try a lower number: ")

            st -= gained_points
            points += gained_points
            print("Your have gained ", gained_points, " points and decreased your Strength to ", st)

     #The process is the same for all the different stats
     if chosen_stat == "2":
        print("\nYou have selected Dexterity.")
        print("Press 1 to increase it")
        print("Press 2 to decrease it")
        up_or_down_dx = input("")
        if up_or_down_dx == "1":
            spent_points = int(input("How many points will you spend?: ")
            while spent_points > points:
                               print("You don't have that many points.")
                               spent_points = int(input("Try a lower number: ")

            dx += spent_points
            points -= spent_points
            print("Your have spent ", spent_points, " and increased your Strength to ", dx)

        if up_or_down_dx == "2":
            gained_points = int(input("How many points will you deduct?: ")
            while gained_points > dx:
                               print("You don't have that many points.")
                               spent_points = int(input("Try a lower number: ")

            dx -= gained_points
            points += gained_points
            print("Your have gained ", gained_points, " points and decreased your Dexterity to ", dx)

     if chosen_stat == "3":
        print("\nYou have selected Health.")
        print("Press 1 to increase it")
        print("Press 2 to decrease it")
        up_or_down_ht = input("")
        if up_or_down_ht == "1":
            spent_points = int(input("How many points will you spend?: ")
            while spent_points > points:
                               print("You don't have that many points.")
                               spent_points = int(input("Try a lower number: ")

            ht += spent_points
            points -= spent_points
            print("Your have spent ", spent_points, " and increased your Health to ", ht)

        if up_or_down_ht == "2":
            gained_points = int(input("How many points will you deduct?: ")
            while gained_points > ht:
                               print("You don't have that many points.")
                               spent_points = int(input("Try a lower number: ")

            ht -= gained_points
            points += gained_points
            print("Your have gained ", gained_points, " points and decreased your Health to ", ht)

     if chosen_stat == "4":
        print("\nYou have selected Wisdom.")
        print("Press 1 to increase it")
        print("Press 2 to decrease it")
        up_or_down_ws = input("")
        if up_or_down_ws == "1":
            spent_points = int(input("How many points will you spend?: ")
            while spent_points > points:
                               print("You don't have that many points.")
                               spent_points = int(input("Try a lower number: ")

            ws += spent_points
            points -= spent_points
            print("Your have spent ", spent_points, " and increased your Wisdom to ", ws)

        if up_or_down_ws == "2":
            gained_points = int(input("How many points will you deduct?: ")
            while gained_points > ws:
                               print("You don't have that many points.")
                               spent_points = int(input("Try a lower number: ")

            ws -= gained_points
            points += gained_points
            print("Your have gained ", gained_points, " points and decreased your Wisdom to ", ws)

#Show the final character when all the points are gone
print("You have spent all your points.")
print("Here is your completed character:")
print("Your Strength is: ", st)
print("Your Dexterity is: ", dx)
print("Your Health is: ", ht)
print("Your Wisdom is: ", ws)

input("\n\nPress the enter key to exit")

我知道它可能非常不优雅,但这是我用我有限的经验做的最好的。此行发生错误:

while spent_points > points:

错误说"语法无效"。我不知道这意味着什么或我做错了什么。错误的突出显示文本只是"而#34;部分。我试过改变"而#34;到"如果"看看那些固定的东西,然后错误出现在":"该行末尾的字符,但消息是相同的。

我已经在本网站的其他地方看过,并在突出显示的文字之前阅读了有关此问题的证据,但没有更具体的内容。

我非常感谢任何协助。

1 个答案:

答案 0 :(得分:3)

您只有语法错误,上面有以下行:

spent_points = int(input("How many points will you spend?: ")

将此更改为

spent_points = int(input("How many points will you spend?: "))

请注意,每次使用模式int(输入(...)时都会发生这种情况。所以在所有情况下添加额外的括号,你应该好好去。

另外,我应该注意,一旦你过去,你将使用if chosen_stat ==“2”:语句遇到IndentionError。看起来你在“if”语句之前隐藏了一个空格。在每个退出错误前一次退格。