【问题标题】:I'm starting out with python and keep getting a problem with indents我从 python 开始,一直遇到缩进问题
【发布时间】:2021-08-26 21:16:03
【问题描述】:

这是整个代码,我用粗体突出了问题的主要部分。额外信息,我使用的是 3.5.9 版本,我只是使用 IDLE,而不是 IDLEx,因为它不起作用。

import time
import sys

def play_game_yn (): #asks if wants to play game
time.sleep (1.5)
play_game=input ('Do you want to play a game? (y/n)')

    if play_game=='Y':
        play_yes ()
    elif play_game=='y':
        play_yes ()
    **elif play_game=='N':
    play_game_sure=input ("Are you sure you don't want to play? (y/n)") #asks if sure don't want play
        if play_game_sure=='Y':
            play_no ()
        elif play_game_sure=='y':
            play_no ()
        elif play_game_sure=='N':
            play_yes ()
        elif play_game_sure=='n':
            play_yes ()
        else:
            print ("Please enter 'y'or'n'") 
            time.sleep (2)
            play_game_yn ()
    elif play_game=='N':
        play_game_sure=input ("Are you sure you don't want to play? (y/n)") #asks if sure don't want play
        if play_game_sure=='Y':
            play_no ()
        elif play_game_sure=='y':
            play_no ()
        elif play_game_sure=='N':
            play_yes ()
        elif play_game_sure=='n':
            play_yes ()
        else:
            print ("Please enter 'y'or'n'") 
            time.sleep (2)
            play_game_yn ()**
    else:
        print ("Please enter 'y'or'n'") 
        time.sleep (2)
        play_game_yn ()


def play_yes(): #play yes
    print ("Woohoo,")
    time.sleep (0.5)
    game=input ("What game do you want to play?") #asks what game want play
    print ("Uhmm...")
    time.sleep (2)
    print ("Let me think...")
    time.sleep (1)
    print ("Nope, doesn't ring any bells")
    time.sleep (0.75)
    print ("Sorry,", name + ", I have no clue how to play", game + "")
    time.sleep (5)
    sys.exit()

def play_no (): #play no
    print ('Awwh, it was fun while it lasted')
    time.sleep (1)
    print ('See ya around,',name + '')
    tine.sleep (2)
    sys.exit()

name=input ("Hey there, I'm SIRB, Super Intelligent Robot Boy, what's your name?") #intriduces sirb & ask name

if name=='harry':
    print ("hey, that's the same name as the person who made me :)")
    time.sleep (2)
    print ('Hello, ' + name + ', it is nice to meet you')
    play_game_yn ()
elif name=='Harry':
    print ("hey, that's the same name as the person who made me :)")
    time.sleep
    print ('Hello, ' + name + ', it is nice to meet you')
    play_game_yn ()
else:
    print ('Hello, ' + name + ', that is a nice name, nice to meet you')
    play_game_yn ()

ps,你如何得到显示列表的垂直线? pps,真的很抱歉这是多少代码,我对python很陌生,只是为了这个问题而创建了这个帐户

【问题讨论】:

  • “它不起作用”不是描述。 minimal reproducible example
  • elif play_game=='N':play_game_sure=input(...) 是否缩进到同一级别?如果是这样,您需要将第二行再缩进一级。
  • tine.sleep (2) 有错字。此外,您的间距似乎确实不一致;我强烈推荐使用像 PyCharm 这样的免费的以 Python 为中心的 IDE,它可以真正帮助确保所有缩进都是正确的,并实时显示语法错误等。缩进是任何 Python 程序流程的核心部分;你必须确切地知道它是如何工作的,否则你将很难过。例如,你不能无缘无故地随机缩进一些代码——那会立即引发语法错误。而且,每个elif 的缩进必须与if 完全匹配。
  • 你如何得到显示列表的垂直线? 在 IDLE 中,你不能。当某些行有>>> 提示而其他行没有提示时,排列起来会很困难。 IDLE 不适用于编写需要缩进指南的长程序。使用 PyCharm(社区版免费)或 Notepad++(免费)。

标签: python python-3.5


【解决方案1】:

我更正了缩进,还有一件事是第 52 行中缺少 time.sleep 的值,我给出的值为 2,请根据您的要求进行更改。

import time
import sys

def play_game_yn (): #asks if wants to play game
    time.sleep (1.5)
    play_game=input ('Do you want to play a game? (y/n)')

    if play_game=='Y':
        play_yes ()
    elif play_game=='y':
        play_yes ()
    elif play_game=='N':
        play_game_sure=input ("Are you sure you don't want to play? (y/n)") #asks if sure don't want play
        if play_game_sure=='Y':
            play_no ()
        elif play_game_sure=='y':
            play_no ()
        elif play_game_sure=='N':
            play_yes ()
        elif play_game_sure=='n':
            play_yes ()
        else:
            print ("Please enter 'y'or'n'")
            time.sleep (2)
            play_game_yn ()

    elif play_game=='N':
        play_game_sure=input ("Are you sure you don't want to play? (y/n)") #asks if sure don't want play
        if play_game_sure=='Y':
            play_no ()
        elif play_game_sure=='y':
            play_no ()
        elif play_game_sure=='N':
            play_yes ()
        elif play_game_sure=='n':
            play_yes ()
        else:
            print ("Please enter 'y'or'n'")
            time.sleep (2)
            play_game_yn ()
    else:
        print ("Please enter 'y'or'n'")
        time.sleep (2)
        play_game_yn ()


def play_yes(): #play yes
    print ("Woohoo,")
    time.sleep (0.5)
    game=input ("What game do you want to play?") #asks what game want play
    print ("Uhmm...")
    time.sleep (2)
    print ("Let me think...")
    time.sleep (1)
    print ("Nope, doesn't ring any bells")
    time.sleep (0.75)
    print ("Sorry,", name + ", I have no clue how to play", game + "")
    time.sleep (5)
    sys.exit()

def play_no (): #play no
    print ('Awwh, it was fun while it lasted')
    time.sleep (1)
    print ('See ya around,',name + '')
    time.sleep (2)
    sys.exit()

name=input ("Hey there, I'm SIRB, Super Intelligent Robot Boy, what's your name?") #intriduces sirb & ask name

if name=='harry':
    print ("hey, that's the same name as the person who made me :)")
    time.sleep (2)
    print ('Hello, ' + name + ', it is nice to meet you')
    play_game_yn ()
elif name=='Harry':
    print ("hey, that's the same name as the person who made me :)")
    time.sleep(2)
    print ('Hello, ' + name + ', it is nice to meet you')
    play_game_yn ()
else:
    print ('Hello, ' + name + ', that is a nice name, nice to meet you')
    play_game_yn ()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-28
    • 2018-07-18
    • 1970-01-01
    • 2017-04-21
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多