我可以在pythontutor和PyCharm上运行我的代码,而且它可以正常工作。 但是,当我尝试在Jupyter Notebook中运行代码时,我一直收到SyntaxError的错误。
我已经搜索了网络,唯一找到的与CodeMirror问题有关。 不确定那是什么。 我在f-string使用上做错了什么?
def list_o_matic(blank):
if new_animal == "": #check to see if nothing was entered if so then pop last animal in list
pop = animal.pop()
msg_pop = f'{pop} popped from list' # popped for list
return msg_pop
else:
if new_animal in animal: #check to see if already have animal if yes remove animal from list
remove = animal.remove(new_animal)
msg_remove = f'1 instance of {new_animal} removed from list'
return msg_remove
else:
animal.append(new_animal) #check to see if animal is not in list if so append list to add animal
msg_append = f'1 instance of {new_animal} appended to list'
return msg_append
animal = ['cat','goat','cat']
while True:
if len(animal) == 0: #check if list is empty
print("\nGoodbye!")
break
else: # get animal input
print("look at all the animals ",animal)
new_animal = input("enter the name of an animal or quit: ").lower()
if new_animal == "quit": # quit entered print msg and leave
print("\nGoodbye!")
break
else:
print(list_o_matic(new_animal),"\n")
文件“”,第6行 msg_pop = f'从列表中弹出{pop}' # 弹出来自列表 ^ 语法错误:无效的语法
if
循环,并运行print(list_o_matic('cat'))
来大幅简化您的代码,看看会发生什么。 - Jack Fleeting