我为一个类分配创建了一个简单的程序:
>>> 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错误:
File 'directions.py", line 1
>>>print("Ready?")
^
SyntaxError: invalid syntax
我已经尝试了很多方法,但总有些困难。很抱歉,如果这是基本的东西,我才刚刚开始学习Python。
谢谢。
发布于 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'
。
发布于 2020-05-14 19:53:56
像这样
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 ...
https://stackoverflow.com/questions/61796262
复制相似问题