Python 错误:取消缩进与任何外部取消缩进限制不匹配

发布于 2024-11-13 11:02:27 字数 800 浏览 3 评论 0原文

正如你所看到的,我是一个编程新手,并且是从 Python 开始的,上述错误发生在突出显示的代码行上。 怎么骑过去这个...

import random

secret= random.randint (1,100)
guess=0
tries=0

print "AHOY! I am the dead pirate Roberts, and I ahve a secret!"
print  "It is a number from 1 to 99. I will give you six tries."

while guess  !=secret and tries <6:
    guess= input("what's yer guess? " )
    if  guess < secret :
                 print "Too Low, ye curvy dog!"
    elif guess > secret:
                 print "Too high, landlubber!"
                 tries= tries +1

         ***if  guess == secret  :***
             print "Avast! Ye got it! found my secret, ye did!"
                 else:
             print "No more guesses! Better luck next time, matey!"
             print "The secret number was ", secret" 

As you can see I am a novice in Programming and started with Python, the above said error is occurring on the highlighted line of code.
How to ride over this...

import random

secret= random.randint (1,100)
guess=0
tries=0

print "AHOY! I am the dead pirate Roberts, and I ahve a secret!"
print  "It is a number from 1 to 99. I will give you six tries."

while guess  !=secret and tries <6:
    guess= input("what's yer guess? " )
    if  guess < secret :
                 print "Too Low, ye curvy dog!"
    elif guess > secret:
                 print "Too high, landlubber!"
                 tries= tries +1

         ***if  guess == secret  :***
             print "Avast! Ye got it! found my secret, ye did!"
                 else:
             print "No more guesses! Better luck next time, matey!"
             print "The secret number was ", secret" 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红玫瑰 2024-11-20 11:02:27

Python 使用缩进来表示代码块。代码的缩进无效(相关 if 的缩进与之前的任何块都不对齐;快速浏览一下代码,至少还有一个错误)。

以下是 Python 中缩进工作原理的简短清晰解释: http://diveintopython.net/getting_to_know_python/indenting_code .html

Python uses indentation to denote code blocks. The indentation of your code is not valid (the indentation of the if in question doesn't line up with any previous block; from a quick look over the code, there's at least one more error).

The following is a short clear explanation of how indentation works in Python: http://diveintopython.net/getting_to_know_python/indenting_code.html

爱你不解释 2024-11-20 11:02:27

在 python 中,块之间必须保持恒定的缩进。

if 猜测 中Secret: 代码块,缩进比 while 代码块中的缩进长得多。

正确的代码是:

while guess  !=secret and tries <6:
    guess= input("what's yer guess? " )
    if  guess < secret :
        print "Too Low, ye curvy dog!"
    elif guess > secret:
        print "Too high, landlubber!"
        tries= tries +1

    if  guess == secret  :
        print "Avast! Ye got it! found my secret, ye did!"
    else:
        print "No more guesses! Better luck next time, matey!"
        print "The secret number was ", secret" 

In python, you must keep a constant indentation between your blocks.

And in the if guess < secret: code block, the indentation is much longer than in the while code block.

The proper code is :

while guess  !=secret and tries <6:
    guess= input("what's yer guess? " )
    if  guess < secret :
        print "Too Low, ye curvy dog!"
    elif guess > secret:
        print "Too high, landlubber!"
        tries= tries +1

    if  guess == secret  :
        print "Avast! Ye got it! found my secret, ye did!"
    else:
        print "No more guesses! Better luck next time, matey!"
        print "The secret number was ", secret" 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文