字符串中的内括号一直变成红色,我认为它给了我一个语法错误,特别是当我在终端命令提示符下运行它时的第一个打印语句。
print(f"Hi {user_name}, Im the {script} script")
^
SyntaxError: invalid syntax
我尝试使用Google搜索,替换括号(尝试和错误)并尝试使用youtube,认为这样做会更好
from sys import argv
script, user_name=argv
prompt="> "
print(f"Hi {user_name}, im the {script} script")
print("Id like to ask you a few questions")
print(f"do you like me {user_name}?")
likes=input(prompt)
print(f"where do you live {user_name}?")
lives=input(prompt)
print("what kind of computer do you have?")
computer=input(prompt)
print(f"""
Alright, so you said {likes} about liking me.
You live in {lives}. Not sure where that is.
And you have a {computer} computer. Nice
""")
答案 0 :(得分:1)
在Python 3.7上不会出现该语法错误。您很有可能尝试使用不具有新f字符串语法的旧版本的Python运行脚本。
在macOS上,请注意,默认情况下,python
命令引用的是Python版本2。请尝试使用python3
运行脚本。
如果仅在终端机上输入python
,则>>>
提示符前的标语将告诉您确切的版本。与python3
相同。
请注意,您必须至少具有3.6版才能使用f字符串。
答案 1 :(得分:0)
我认为您只会在尝试输入名称而不是单词时遇到错误。所以我做了一些调整。看看是否是这种情况:
from sys import argv
script = argv[0]
if len(argv) < 2:
print("you must give me a name as commandline param to work with")
exit(0)
user_name=' '.join(argv[1:])
prompt="> "
print(f"Hi {user_name}, im the {script} script")
print("Id like to ask you a few questions")
print(f"do you like me {user_name}?")
likes=input(prompt)
print(f"where do you live {user_name}?")
lives=input(prompt)
print("what kind of computer do you have?")
computer=input(prompt)
print(f"""
Alright, so you said {likes} about liking me.
You live in {lives}. Not sure where that is.
And you have a {computer} computer. Nice
""")