首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何纠正prompt SyntaxError?

如何纠正prompt SyntaxError?
EN

Stack Overflow用户
提问于 2020-05-14 19:29:38
回答 2查看 50关注 0票数 1

我为一个类分配创建了一个简单的程序:

代码语言:javascript
运行
复制
>>> print("Ready?")
Ready?
>>> x = "yes"
>>> y = "no"
>>> if "x":
    print("Turn out of driveway, heading west on T Street for .8 miles, while maintaining a speed of 25mph, until reaching the first Stop Sign at the intersection.")


Turn out of driveway, heading west on T Street for .8 miles, while maintaining a speed of 25mph, until reaching the first Stop Sign at the intersection.
>>> if "y":
    print("Then this script isn't for you.")

Then this script isn't for you.
>>> print("Coming to a stop at first Stop Sign, turn right onto U Street and drive for .6 miles while maintaining a speed of 25mph, until you reach the first Traffic Light.")
Coming to a stop at first Stop Sign, turn right onto U Street and drive for .6 miles while maintaining a speed of 25mph, until you reach the first Traffic Light.
>>> print("Once you reach the first Traffic Light, turn left onto V Street and drive for .5 miles while maintaining a speed of 25mph, until you reach the second Traffic Light.")
Once you reach the first Traffic Light, turn left onto V Street and drive for .5 miles while maintaining a speed of 25mph, until you reach the second Traffic Light.
>>> print("Then, once you reach the second Traffic Light, turn right and drive the remaining .1 mile, maintaining a speed of 25mph, until you reach your destination on the right.")
Then, once you reach the second Traffic Light, turn right and drive the remaining .1 mile, maintaining a speed of 25mph, until you reach your destination on the right.
>>> 

但是,当我尝试在终端中使用"python3 directions.py“命令运行它时,我得到了以下Sytanx错误:

代码语言:javascript
运行
复制
File 'directions.py", line 1
 >>>print("Ready?")
 ^
SyntaxError: invalid syntax

我已经尝试了很多方法,但总有些困难。很抱歉,如果这是基本的东西,我才刚刚开始学习Python。

谢谢。

EN

回答 2

Stack Overflow用户

发布于 2020-05-14 19:36:57

>>>是repl shell前缀(您可以在终端中运行python3获得该前缀)。此符号仅用于大于号/小于号,您应该从每行的开头删除它。

此外,if 'yes'if 'x'if 'y'if 'no'的计算结果始终为True。您应该使用一个输入函数- x = input('yes or no')并检查它:if x.lower() == 'yes'

票数 1
EN

Stack Overflow用户

发布于 2020-05-14 19:53:56

像这样

代码语言:javascript
运行
复制
answer = input("Ready? yes/no ")
# y/Y or ye?YE or yes/YES are accepted
if(answer.lower() in "yes"):
  print("Turn out of driveway, heading west on T Street for .8 miles, while maintaining a speed of 25mph, until reaching the first Stop Sign at the intersection.")
else:
  print("Then this script isn't for you.")
  # print other output ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61796262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档