2017-05-05 8 views
0

私はプログラミングが初めてです。私は1ヵ月ほど前から真剣に勉強し始めました。Python for Absolute Beginnerロードブロッキング;第5章、チャレンジ#2 - 無効な構文

私が読んできた本は、絶対初心者のためのPythonであり、私は第5章のチャレンジセクションの不調を打ちました。私はこの本を一定のペースで進めてきましたが、以前は1つの課題にしか失敗していませんでした。この新しいものは私に「無効な構文」のエラーを与えています。なぜそれがわかりません。

私の(確かに経験の浅い)心にはすべてが整然としているようです。 私は間違ったことを理解できません。ここで

コードです:

#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"に変更しようとしましたが、メッセージは同じでした。

私はこのサイト上の他の場所で見て、それよりも、より具体的な強調表示されたテキスト前に、問題のこのことについて何か証拠が、何を読みました。

ご協力いただきまして大変感謝します。

+0

括弧がないと、間違いやすいです。私はいつもそれをする。あなたの準備ができたら、専用のPython IDEを入手することをお勧めします。彼らはこの種のものについて警告します。その他のランタイム前エラーが発生します。人気のあるものは[PyCharm](https://www.jetbrains.com/pycharm/)です。 –

答えて

3

は、あなたはそれ以上の次の行で構文エラーがあります。これはあなたがパターンint型(入力を(持っているすべての時間に発生していること

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

ノートへ

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

変更これを..すべてのケースに余分な括弧を追加してください。

さらに、一度あなたがあなたのif_startでIndentionErrorを実行すると、 at == "2":ステートメント。 "if"ステートメントの前に隠されたスペースがあるように見えます。エラーを取り除くために、それぞれの前に1回バックスペースします。

+0

ありがとうございます!とても簡単。私のルーキーミス。 –

+0

甘い、全く問題ありません。あなたは答えを受け入れたものとしてマークできますか?ありがとう! –

+0

@TimtheEnchanter、回答を受け入れ可能とマークできますか?感謝します。 –