代码错误是什么:在时间方法中获取无效语法

What is the error the code: getting invalid syntax in time method

#multiplication table with time delay
import time

num=int(input("Enter the value for which you want the multiplication table for:"))

print("The table will be as:\n")
for i in range(1,11):
    {
        print(num,"x",i,"=",num*i,"\n")
        time.sleep(3)
    }

print("The table is completed")
input("Press enter to exit")

在这段代码中当编译器到达这条语句时

time.sleep(3)

它显示 无效语法 错误。这段代码有什么错误?

它在抱怨 {}s。在Python中,他们没有定义一个块,他们定义了一个字典。因此,您在 {} 中所拥有的应该是字典文字,但那不是您所拥有的。你可能想要:

for i in range(1, 11):
    print(num, "x", i, "=", num * i, "\n")
    time.sleep(3)

(注意,缩进定义块。)