菜鸟在字典中有语法错误 (python)
Noob with a syntax error in dictionary (python)
我正在尝试学习 Python 遵循使用 Mosh 编程的教程。
我创建了一个程序,它会启动并问候我,然后让我select 列表中的一个项目。每个列表项都是教程涵盖的“子程序”。所以我的程序中有两个子程序:
- 一个聊天翻译器,应该 return 文本并通过字典将 :) 和 :( 转换为表情符号
- 然后还有一个方形计算器。
我面临的问题是我的聊天翻译器在词典中遇到语法错误。这是错误发生的地方,在字典第 2 行的第一个引号处:
C:\Users\smelt\PycharmProjects\HelloWorld\venv\Scripts\python.exe C:/Users/smelt/PycharmProjects/HelloWorld/venv/app.py
File "C:\Users\smelt\PycharmProjects\HelloWorld\venv\app.py", line 41
":(": emoji.emojize(":frowning_face:")
^
SyntaxError: invalid syntax
错误发生的部分代码:
def emoji_converter(message):
words = message.split(" ")
emojis = {
":)": emoji.emojize(":smiling_face:"),
":(": emoji.emojize(":frowning_face:")
}
output = ""
for word in words:
output += emojis.get(word, word) + " "
return output
这是我的全部代码:
first_name = "Henrik"
last_name = "Skaaning"
program_on = True
calculator_on = False
emoji_converter_on = False
def greet_user(first_name, last_name):
# Standard greeting to user
print(f"""Hi {first_name} {last_name}!
Welcome back!
""")
if program_on:
while program_on:
greet_user(first_name, last_name)
selection = input(f"""This is Training program for Henrik Skaaning.
please enter a number to select a program to run
1: Emoji converter
2: Square calculator
enter "quit" to quit program...
selection> """)
if selection == "1":
emoji_converter_on = True
print(f'Emoji converter on')
while emoji_converter_on:
import emoji
def emoji_converter(message):
words = message.split(" ")
emojis = {
":)": emoji.emojize(":smiling_face:"),
":(": emoji.emojize(":frowning_face:")
}
output = ""
for word in words:
output += emojis.get(word, word) + " "
return output
message = input("message> ")
if message != "help":
if message != "quit":
print(emoji_converter(message))
if message == "help":
print(f"""This is a simple emoji converter developed by Henrik Skaaning.
Type a text in the command line with an emoji to return the text and emoji.
Type "help" in the command line to return the help-menu.
Type "quit" in the command line to quit the application. """)
if message == "quit":
emoji_converter_on = False
print(f'Emoji converter shutting off')
if selection == "2":
calculator_on = True
print(f'Square calculator on')
while calculator_on:
def square(number):
return int(number) * int(number)
number = input("commandline> ")
if number == "quit":
program_on = False
calculator_on = False
print(f'Executing')
if number != "quit":
if number != "help":
if number.isnumeric() != True:
print(f"Sorry! That isnt a command i understand")
if number == "help":
print(f"""This is a simple square calculator developed by Henrik Skaaning.
Type a number in the command line to return the square of that number
Type "help" in the command line to return the help-menu.
Type "quit" in the command line to quit the application. """)
if number.isnumeric():
result = square(number)
print(f' The result is {result}')
if program_on == False:
print(f'Program shut down')
print(f'Done...')
虽然可以在另一个函数中定义一个函数,有时使用闭包会使事情变得更容易,但此代码的主线非常长,其中一个函数在 while 循环中定义。如果将函数分开编写,这段代码会更好、更容易理解。否则,正如您现在所知,可能很难调试。
实际问题是您混淆了 Python dict
和 JSON 的语法。 dict
需要符号/值对,而 JSON 需要字符串/值对。
下面是为您重新组织的程序。该错误未得到纠正。
first_name = "Henrik"
last_name = "Skaaning"
program_on = True
calculator_on = False
emoji_converter_on = False
def emoji_converter(message):
words = message.split(" ")
emojis = {
":)": emoji.emojize(":smiling_face:"),
":(": emoji.emojize(":frowning_face:")
}
output = ""
for word in words:
output += emojis.get(word, word) + " "
return output
def greet_user(first_name, last_name):
# Standard greeting to user
print(f"""Hi {first_name} {last_name}!
Welcome back!
""")
if program_on:
while program_on:
greet_user(first_name, last_name)
selection = input(f"""This is Training program for Henrik Skaaning.
please enter a number to select a program to run
1: Emoji converter
2: Square calculator
enter "quit" to quit program...
selection> """)
if selection == "1":
emoji_converter_on = True
print(f'Emoji converter on')
while emoji_converter_on:
import emoji
message = input("message> ")
if message != "help":
if message != "quit":
print(emoji_converter(message))
if message == "help":
print(f"""This is a simple emoji converter developed by Henrik Skaaning.
Type a text in the command line with an emoji to return the text and emoji.
Type "help" in the command line to return the help-menu.
Type "quit" in the command line to quit the application. """)
if message == "quit":
emoji_converter_on = False
print(f'Emoji converter shutting off')
if selection == "2":
calculator_on = True
print(f'Square calculator on')
while calculator_on:
def square(number):
return int(number) * int(number)
number = input("commandline> ")
if number == "quit":
program_on = False
calculator_on = False
print(f'Executing')
if number != "quit":
if number != "help":
if number.isnumeric() != True:
print(f"Sorry! That isnt a command i understand")
if number == "help":
print(f"""This is a simple square calculator developed by Henrik Skaaning.
Type a number in the command line to return the square of that number
Type "help" in the command line to return the help-menu.
Type "quit" in the command line to quit the application. """)
if number.isnumeric():
result = square(number)
print(f' The result is {result}')
if program_on == False:
print(f'Program shut down')
print(f'Done...')
既然你知道你需要一个符号而不是像“:)”和“:(”这样的字符串,你能自己解决吗?
提示:使用字符串替换而不是 dict
。将所有 ":)"
字符串转换为 ":smiling_face:"
,将 ":("
字符串转换为 ":frowning_face:"
,例如:
message.replace(":)", ":smiling_face:").replace(":(", ":frowning_face:")
更正后的程序应该明显更短。
您发布的代码不是您 运行 生成该错误消息的代码。它没有给出语法错误,错误中的行现在位于第 33 行,而不是堆栈跟踪中的第 41 行。
我猜你错过了第一个字典条目后的逗号
emojis = {
":)": emoji.emojize(":smiling_face:")
":(": emoji.emojize(":frowning_face:")
}
给出你得到的错误。
我正在尝试学习 Python 遵循使用 Mosh 编程的教程。
我创建了一个程序,它会启动并问候我,然后让我select 列表中的一个项目。每个列表项都是教程涵盖的“子程序”。所以我的程序中有两个子程序:
- 一个聊天翻译器,应该 return 文本并通过字典将 :) 和 :( 转换为表情符号
- 然后还有一个方形计算器。
我面临的问题是我的聊天翻译器在词典中遇到语法错误。这是错误发生的地方,在字典第 2 行的第一个引号处:
C:\Users\smelt\PycharmProjects\HelloWorld\venv\Scripts\python.exe C:/Users/smelt/PycharmProjects/HelloWorld/venv/app.py
File "C:\Users\smelt\PycharmProjects\HelloWorld\venv\app.py", line 41
":(": emoji.emojize(":frowning_face:")
^
SyntaxError: invalid syntax
错误发生的部分代码:
def emoji_converter(message):
words = message.split(" ")
emojis = {
":)": emoji.emojize(":smiling_face:"),
":(": emoji.emojize(":frowning_face:")
}
output = ""
for word in words:
output += emojis.get(word, word) + " "
return output
这是我的全部代码:
first_name = "Henrik"
last_name = "Skaaning"
program_on = True
calculator_on = False
emoji_converter_on = False
def greet_user(first_name, last_name):
# Standard greeting to user
print(f"""Hi {first_name} {last_name}!
Welcome back!
""")
if program_on:
while program_on:
greet_user(first_name, last_name)
selection = input(f"""This is Training program for Henrik Skaaning.
please enter a number to select a program to run
1: Emoji converter
2: Square calculator
enter "quit" to quit program...
selection> """)
if selection == "1":
emoji_converter_on = True
print(f'Emoji converter on')
while emoji_converter_on:
import emoji
def emoji_converter(message):
words = message.split(" ")
emojis = {
":)": emoji.emojize(":smiling_face:"),
":(": emoji.emojize(":frowning_face:")
}
output = ""
for word in words:
output += emojis.get(word, word) + " "
return output
message = input("message> ")
if message != "help":
if message != "quit":
print(emoji_converter(message))
if message == "help":
print(f"""This is a simple emoji converter developed by Henrik Skaaning.
Type a text in the command line with an emoji to return the text and emoji.
Type "help" in the command line to return the help-menu.
Type "quit" in the command line to quit the application. """)
if message == "quit":
emoji_converter_on = False
print(f'Emoji converter shutting off')
if selection == "2":
calculator_on = True
print(f'Square calculator on')
while calculator_on:
def square(number):
return int(number) * int(number)
number = input("commandline> ")
if number == "quit":
program_on = False
calculator_on = False
print(f'Executing')
if number != "quit":
if number != "help":
if number.isnumeric() != True:
print(f"Sorry! That isnt a command i understand")
if number == "help":
print(f"""This is a simple square calculator developed by Henrik Skaaning.
Type a number in the command line to return the square of that number
Type "help" in the command line to return the help-menu.
Type "quit" in the command line to quit the application. """)
if number.isnumeric():
result = square(number)
print(f' The result is {result}')
if program_on == False:
print(f'Program shut down')
print(f'Done...')
虽然可以在另一个函数中定义一个函数,有时使用闭包会使事情变得更容易,但此代码的主线非常长,其中一个函数在 while 循环中定义。如果将函数分开编写,这段代码会更好、更容易理解。否则,正如您现在所知,可能很难调试。
实际问题是您混淆了 Python dict
和 JSON 的语法。 dict
需要符号/值对,而 JSON 需要字符串/值对。
下面是为您重新组织的程序。该错误未得到纠正。
first_name = "Henrik"
last_name = "Skaaning"
program_on = True
calculator_on = False
emoji_converter_on = False
def emoji_converter(message):
words = message.split(" ")
emojis = {
":)": emoji.emojize(":smiling_face:"),
":(": emoji.emojize(":frowning_face:")
}
output = ""
for word in words:
output += emojis.get(word, word) + " "
return output
def greet_user(first_name, last_name):
# Standard greeting to user
print(f"""Hi {first_name} {last_name}!
Welcome back!
""")
if program_on:
while program_on:
greet_user(first_name, last_name)
selection = input(f"""This is Training program for Henrik Skaaning.
please enter a number to select a program to run
1: Emoji converter
2: Square calculator
enter "quit" to quit program...
selection> """)
if selection == "1":
emoji_converter_on = True
print(f'Emoji converter on')
while emoji_converter_on:
import emoji
message = input("message> ")
if message != "help":
if message != "quit":
print(emoji_converter(message))
if message == "help":
print(f"""This is a simple emoji converter developed by Henrik Skaaning.
Type a text in the command line with an emoji to return the text and emoji.
Type "help" in the command line to return the help-menu.
Type "quit" in the command line to quit the application. """)
if message == "quit":
emoji_converter_on = False
print(f'Emoji converter shutting off')
if selection == "2":
calculator_on = True
print(f'Square calculator on')
while calculator_on:
def square(number):
return int(number) * int(number)
number = input("commandline> ")
if number == "quit":
program_on = False
calculator_on = False
print(f'Executing')
if number != "quit":
if number != "help":
if number.isnumeric() != True:
print(f"Sorry! That isnt a command i understand")
if number == "help":
print(f"""This is a simple square calculator developed by Henrik Skaaning.
Type a number in the command line to return the square of that number
Type "help" in the command line to return the help-menu.
Type "quit" in the command line to quit the application. """)
if number.isnumeric():
result = square(number)
print(f' The result is {result}')
if program_on == False:
print(f'Program shut down')
print(f'Done...')
既然你知道你需要一个符号而不是像“:)”和“:(”这样的字符串,你能自己解决吗?
提示:使用字符串替换而不是 dict
。将所有 ":)"
字符串转换为 ":smiling_face:"
,将 ":("
字符串转换为 ":frowning_face:"
,例如:
message.replace(":)", ":smiling_face:").replace(":(", ":frowning_face:")
更正后的程序应该明显更短。
您发布的代码不是您 运行 生成该错误消息的代码。它没有给出语法错误,错误中的行现在位于第 33 行,而不是堆栈跟踪中的第 41 行。
我猜你错过了第一个字典条目后的逗号
emojis = {
":)": emoji.emojize(":smiling_face:")
":(": emoji.emojize(":frowning_face:")
}
给出你得到的错误。