如何在Python中创建函数?

时间:2018-03-03 04:04:59

标签: python python-3.x function

我是编码的新手,我正在学习python。我正在尝试编写一个简单的程序来测试我的技能,但我遇到了一些困难;我想把它变成一个函数,以使程序更清晰,但我收到此错误:http://prntscr.com/im5pt7

这是我想要放在一个函数中的内容:

rgba(255,255,255,.65)

我已经尝试了this,但它没有用。 这是所有代码(工作但没有函数,所以我需要复制和粘贴代码):https://pastebin.com/PA9mxMkk

3 个答案:

答案 0 :(得分:1)

正在发生的事情是,其他语句的功能需要将其代码保存到新的缩进级别

print('a')

def test(var):
    print(var)

不是这样的

print('a')
def test(var):
print(var)

因为这样会让你看到错误。

答案 1 :(得分:1)

在理解缩进时学习python通常很难的第一步。

例如。

def hello_world(world):
    print("hello ", world)
    #your function code goes here.

#you need to indent back to be out of function block.
hello_world("there!")

out: hello there

所以在你的情况下它应该是这样的。

def AnsNo():
    name = input(str("\nFull Name: "))      
    position = input(str("Position at the company: "))
    print("\nConfirm Staff Data:\n")
    name_confirm = "Name: %s"%(name)
    position_confirm = "Position: %s"%(position)
    print(name_confirm)
    print(position_confirm)
    confirmAns = input("Is the information right? (Y/N)")
    if confirmAns == "y" or confirmAns == "Y":
        message = "\nSearching for %s"%(name)
        print(message)
        hoursWorked = int(input("Insert hours worked: "))
        if hoursWorked <= 0:
                print("Please insert a valid number")
        elif hoursWorked > 0:
                print("\nCalculete Paycheck")
                hourRate = int(input("Insert the rate of each hour worked: "))
                bonus = input("If a bonus was given insert it here: ")
                fine = input("If a fine was given insert it here: ")
                print('\n')
                payment = hoursWorked*hourRate-int(fine)+int(bonus)
                paymentMsg = "Your Payment is: $%d"%(payment)
                print(paymentMsg)
    elif confirmAns == "n" or confirmAns == "N":
        print("working")
    else:
        print("Please answer with Y or N")
    return

答案 2 :(得分:1)

所有python代码都应该在':'字符后缩进,在python中缩进应该是4个空格,或者人们使用tab键,你的代码有缩进问题我无法找到;

例如'class'

class this_is_a_class():
    #indentation
    #code goes here
    pass

或'for loop'或'while loop';

numbers = [0,1,2,3,4,5,6,7,8,9]
for number in numbers:
    #indentation
    print(number)

x = 0
while x < 10:
    #indentation
    x += 1
    print('This is going to print 10 times')

或'if statement';

true_boolean = True
if true_boolean:
    #indentation
    print(True)

或'功能';

def function():
    #indentation
    print('You have called a function')

实际上发生了什么,是python通过令牌读取你的代码'令牌'并'解释'你的代码所做的事情。但考虑到你不知道功能是什么;对这一段的光泽。

现在提出有关函数如何工作的问题。一个函数用于组织代码。您可以多次调用函数,这使您的代码更有条理,更易于使用,这就是为什么随着代码的延长,您遇到了这个问题;比如让我说我想打印'你好世界'10次。

我可以在10个单独的行上写下这段代码;

print("hello world")
print("hello world")
#etc... More chance for error

或者我可以使用一个函数并调用它10次;

def say_hello_world():
    #indentation
    print("hello world")

for each_call in range(0,10):
    say_hello_world() #This is the function call

您也可以将'参数传递给函数',例如;

def say_hello(person):
    #indentation
    print('hello', person)

say_hello('Alex')

现在,在这个答案中引用的任何单词都可以使用“python”一词进行谷歌搜索,你可以找到更多关于python如何工作的内容。

我希望这能让你开始使用python。虽然所有这些概念都可以用于其他编程语言。