Python 我有一个语法错误,我不知道该怎么做
我在为初学者编写的编程书中写了一段代码。 这就是它看起来的样子。(我必须扩展这篇文章,因为这个网站对问题帖子太挑剔了。)Python 我有一个语法错误,我不知道该怎么做,python,syntax,Python,Syntax,我在为初学者编写的编程书中写了一段代码。 这就是它看起来的样子。(我必须扩展这篇文章,因为这个网站对问题帖子太挑剔了。) Python是一种以空格分隔的语言,这意味着您需要特别注意缩进。您的缩进不一致,这将不可避免地导致问题 除此之外,您没有指定错误的实际内容。将语法错误信息添加到您的帖子中。以下是您的两个问题: number = int(input("What number do you choose?") if number == random.randint (1, 10) 转换为int
Python是一种以空格分隔的语言,这意味着您需要特别注意缩进。您的缩进不一致,这将不可避免地导致问题
除此之外,您没有指定错误的实际内容。将语法错误信息添加到您的帖子中。以下是您的两个问题:
number = int(input("What number do you choose?")
if number == random.randint (1, 10)
转换为int
的语句缺少右括号,if
语句缺少冒号
最后一个问题与末尾的doubleelse
语句有关。取消最后一个缩进,假设这是您想要的
固定的:
import random
print("You are in a dark room in a mysterious castle.")
print("In front of you are four doors. You must choose one.")
playerChoice = input("Choose 1, 2, 3, or 4...")
if playerChoice == "1":
print("You found a room full of tresure. YOU'RE RICH!!!")
print("GAME OVER, YOU WIN!")
elif playerChoice == "2":
print("The door opens and an angry ogre hits you with his club.")
print("GAME OVER, YOU DIED!")
elif playerChoice == "3":
print("You encounter a sleeping dragon.")
print("You can do either:")
print("1) Try to steal some of the dragon's gold")
print("2) Sneak around the dragon to the exit")
dragonChoice = input("type 1 or 2...")
if dragonChoice == "1":
print("The dragon wakes up and eats you. You are delicious.")
print("GAME OVER, YOU WERE EATEN ALIVE.")
elif dragonChoice == "2":
print(
"You sneak around the dragon and escape the castle, blinking in the sunshine.")
print("GAME OVER, YOU ESCAPED THE CASTLE.")
else:
print("Sorry, you didn't enter 1 or 2.")
elif playerChoice == "4":
print("You enter a room with a sphinx.")
print("It asks you to guess what number it is thinking of, betwwen 1 to 10.")
number = int(input("What number do you choose?"))
if number == random.randint(1, 10):
print("The sphinx hisses in dissapointment. You guessed correctly.")
print("It must let you go free.")
print("GAME OVER, YOU WIN.")
else:
print("The sphinx tells you that your guess is incorrect.")
print("You are now it's prisoner forever.")
print("GAME OVER, YOU LOSE.")
else:
print("sorry, you didn't enter 1, 2, 3, or 4...")
print("YOU TURN BACK AND LEAVE (YOU COWARD)")
您只是在间距方面遇到了一些问题,并且忘记了第30行中的“:”
import random
print("You are in a dark room in a mysterious castle.")
print("In front of you are four doors. You must choose one.")
playerChoice = input("Choose 1, 2, 3, or 4...")
if playerChoice == "1":
print("You found a room full of tresure. YOU'RE RICH!!!")
print("GAME OVER, YOU WIN!")
elif playerChoice == "2":
print("The door opens and an angry ogre hits you with his club.")
print("GAME OVER, YOU DIED!")
elif playerChoice == "3":
print("You encounter a sleeping dragon.")
print("You can do either:")
print("1) Try to steal some of the dragon's gold")
print("2) Sneak around the dragon to the exit")
dragonChoice = input("type 1 or 2...")
if dragonChoice == "1":
print("The dragon wakes up and eats you. You are delicious.")
print("GAME OVER, YOU WERE EATEN ALIVE.")
elif dragonChoice == "2":
print("You sneak around the dragon and escape the castle, blinking in the sunshine.")
print("GAME OVER, YOU ESCAPED THE CASTLE.")
else:
print("Sorry, you didn't enter 1 or 2.")
elif playerChoice == "4":
print("You enter a room with a sphinx.")
print("It asks you to guess what number it is thinking of, betwwen 1 to 10.")
number = int(input("What number do you choose?"))
if number == random.randint (1, 10):
print("The sphinx hisses in dissapointment. You guessed correctly.")
print("It must let you go free.")
print("GAME OVER, YOU WIN.")
else:
print("The sphinx tells you that your guess is incorrect.")
print("You are now it's prisoner forever.")
print("GAME OVER, YOU LOSE.")
else:
print("sorry, you didn't enter 1, 2, 3, or 4...")
print("YOU TURN BACK AND LEAVE (YOU COWARD)")
请将代码的缩进修改为程序中实际的缩进。您遇到了什么错误?包括完整的输入和输出。谢谢,我照你说的做了;但我现在在这一部分收到缩进错误:if:number==random.randint(1,10):print(“狮身人面像在绝望中嘶嘶作响。你猜对了。”)print(“它必须让你自由。”)print(“游戏结束,你赢了。”)@NigelAwrey我更新了我的答案,将代码全部包括在内,尽管其中只有一部分存在语法问题。我已经测试过了,运行良好。
import random
print("You are in a dark room in a mysterious castle.")
print("In front of you are four doors. You must choose one.")
playerChoice = input("Choose 1, 2, 3, or 4...")
if playerChoice == "1":
print("You found a room full of tresure. YOU'RE RICH!!!")
print("GAME OVER, YOU WIN!")
elif playerChoice == "2":
print("The door opens and an angry ogre hits you with his club.")
print("GAME OVER, YOU DIED!")
elif playerChoice == "3":
print("You encounter a sleeping dragon.")
print("You can do either:")
print("1) Try to steal some of the dragon's gold")
print("2) Sneak around the dragon to the exit")
dragonChoice = input("type 1 or 2...")
if dragonChoice == "1":
print("The dragon wakes up and eats you. You are delicious.")
print("GAME OVER, YOU WERE EATEN ALIVE.")
elif dragonChoice == "2":
print("You sneak around the dragon and escape the castle, blinking in the sunshine.")
print("GAME OVER, YOU ESCAPED THE CASTLE.")
else:
print("Sorry, you didn't enter 1 or 2.")
elif playerChoice == "4":
print("You enter a room with a sphinx.")
print("It asks you to guess what number it is thinking of, betwwen 1 to 10.")
number = int(input("What number do you choose?"))
if number == random.randint (1, 10):
print("The sphinx hisses in dissapointment. You guessed correctly.")
print("It must let you go free.")
print("GAME OVER, YOU WIN.")
else:
print("The sphinx tells you that your guess is incorrect.")
print("You are now it's prisoner forever.")
print("GAME OVER, YOU LOSE.")
else:
print("sorry, you didn't enter 1, 2, 3, or 4...")
print("YOU TURN BACK AND LEAVE (YOU COWARD)")