Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 什么';缩进怎么了?巨蟒,石头剪刀布游戏_Python_Indentation - Fatal编程技术网

Python 什么';缩进怎么了?巨蟒,石头剪刀布游戏

Python 什么';缩进怎么了?巨蟒,石头剪刀布游戏,python,indentation,Python,Indentation,以下是说明:该程序应使用菜单驱动界面编写。它应该至少包含6个函数,如下所示(如果您认为需要其他函数,您可以拥有更多函数)。您应该选择描述性函数名来正确描述它们的函数(遵循正确命名函数的约定)。为了清楚起见,您不应该让函数名为function1、function2等。请在代码中用您自己的函数名替换下面的名称 当我运行它时,如果comp==1和user==3),我会得到一个错误代码,对行说“预期有一个缩进块” 请告诉我怎么了,谢谢 #import random module import rando

以下是说明:该程序应使用菜单驱动界面编写。它应该至少包含6个函数,如下所示(如果您认为需要其他函数,您可以拥有更多函数)。您应该选择描述性函数名来正确描述它们的函数(遵循正确命名函数的约定)。为了清楚起见,您不应该让函数名为function1、function2等。请在代码中用您自己的函数名替换下面的名称

当我运行它时,如果comp==1和user==3),我会得到一个错误代码,对行
说“预期有一个缩进块”

请告诉我怎么了,谢谢

#import random module
import random
#main function
def main():
    #program message
    print("Rock, Paper, Scissors Game")
    #initializing variables that would hold choices of user and computer
    comp = 1
    user = 1

while comp == user:
    print("Enter your choice in range from 1 to 3")
    #prompt user to enter choice
    user = int(input("Your choice: "))
    #randomly assign choice to computer
    comp = random.randint(1,3)
    #display choice of computer
    print("Computer Choice : ",comp)
    #display game drawn message, when same choices

if (comp == user):
    print("Game Drawn. Select again")
    #calling function to decide winner
    winner (comp, user)
    #winner function
    def winner(comp, user):
    #rock and scissor choice

if(comp == 1 and user ==3):
    print("Computer win")
print("The rock smashes scissor")
    elif(comp == 3 and user ==1):
        print("User win")
        print("The rock smashes scissor")
    else:
    #paper and rock choice

if(comp == 1 and user == 2):
    print("User win")
    print("The paper wraps rock")
        elif (comp == 2 and user ==1):
            print("Computer win")
            print("Scissors cut paper")
        elif (comp == 2 and user == 3):
            print("User win")
            print("Scissors cut paper")
        else:
            print("Invalid selection")
            #calling main function

main()

问题在于前一代码行:

def winner(comp, user):
def
正在定义一个函数,并希望该函数有一个主体。因为您将缩进返回到顶层,所以
def
主体没有任何内容(甚至连
传递都没有),因此不会编译


但请注意,这并不是您唯一的缩进问题。
main
的定义打印一行
print(“石头、布、剪刀游戏”)
,然后设置一些变量和。。。退出。因为当comp==user:
缩进时,
的缩进会退回一个级别,所以它在
main
定义中不存在。这几乎是您的每一个新代码块的问题。你应该仔细阅读如何阅读。

除了纳撒尼尔·福特的答案外,你的if elif else索引在某些地方也是错误的

if (condition):
    print "hi"
elif (condition):
    print "hi again"
else:
    print "hi again vol3"

这是在elif-else条件下独立的正确方法。

它前面的行是一个
def
,它需要一个缩进块,而您没有。这里的缩进问题比我列举的要多。@Fredrarson-更彻底地看一下,您完全正确。如果
结构(以及其他需要不同缩进级别的东西,比如循环)没有自己的作用域,那么它就是各种各样的混乱。你是对的。我将编辑我的帖子以避免混淆。