python - 为什么语法无效(<string>,第 24 行)?

标签 python python-3.x syntax-error

对 Python 非常陌生,我就是不知道是什么导致了我的代码中的错误消息......

It says '[pylint] E0001:invalid syntax (<string>, line 24)'.

有人可以解释一下我在这里缺少什么吗?

非常感谢!

#########################################
# Draws a mario-style right-side-aligned half pyramid 
# of the requested height.
# Restriction: 0 < height < 23
##########################################

while True:
    height = int(input("Height: "))
    if height > 0 and height < 23:
        break
    elif height == 0:
        print()
        print()
        print()
        print("I have drawn a pyramid with a height of 0!")
        print("Isn't it pretty!")
        exit(0)

hashes = 2

for i in range(height):
    spaces = (height - hashes + 1)
    for j in range(spaces):
        print(" ", end="")
    for k in range(hashes):
        print("#", end="" )
    print()
    hashes += 1

最佳答案

您正在使用 python2,应该更改:

print(" ", end="")
print("#", end="" )

至:

print(" "),
print("#"),
<小时/>

此外

你可能应该改变:

print() 

print("")

并且可以通过使用翻译 rowbreak 的“\n”来减少这种“美感”。

print()
print()
print()
print("I have drawn a pyramid with a height of 0!")
print("Isn't it pretty!")

至:

print("\n\n\nI have drawn a pyramid with a height of 0!\nIsn't it pretty!")

关于python - 为什么语法无效(<string>,第 24 行)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46545136/

相关文章:

python - 如何将自定义 Openai 健身房环境与 Openai 稳定基线 RL 算法结合使用?

python 3 : send method of generators

sql - SQL缺少运算符

ios - 无法使用类型为 'findObjectsInBackgroundWithBlock' 的参数列表调用 '(([AnyObject]!, NSError!) -> Void)'

regex - 如何在Groovy的命令行参数中使用星号(*)?

python - Psycopg DB - 向代码添加持久性时出错

python - 如何从继承链中上几层的基类调用方法?

python - scrapy:尝试调试代码的新手

python - 检查一个字符串的字母是否在另一个字符串中

python - celery任务的实时进度跟踪