繁体   English   中英

绝对入门的Python障碍; 第5章,挑战#2-语法无效

[英]Python for the Absolute Beginner roadblock; Chapter 5, Challenge #2 - invalid syntax

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

我读过的书是《 Python for Absolute Beginner》,我在第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:

错误显示“语法无效”。 我不知道这意味着什么,或者我做错了什么。 错误突出显示的文本只是“ while”部分。 我尝试将“ while”更改为“ if”,以查看是否可以解决问题,然后错误出现在行末的“:”字符处,但是消息是相同的。

我在该站点的其他地方进行了浏览,并在突出显示的文本之前阅读了有关此问题的证据,但没有比这更具体的了。

我将不胜感激任何帮助。

您只是在上面的以下行有语法错误:

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

更改为

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

请注意,每次使用模式int(input(...)时,都会发生这种情况,因此请在所有情况下都添加括号,您应该会很高兴。

另外,我应该注意,一旦您过去了,您的if selected_stat ==“ 2”:语句就会遇到IndentionError。 看起来您在“ if”语句之前隐藏了一个空格。 每次退格前要先消除一次错误。

暂无
暂无

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

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