类型错误:create_purple() 采用 0 个位置参数,但给出了 2 个 [英] TypeError: create_purple() takes 0 positional arguments but 2 were given

查看:25
本文介绍了类型错误:create_purple() 采用 0 个位置参数,但给出了 2 个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 编程的新手,正在尝试使该程序正常运行.

I am brand new to Python programming and trying to make this program work.

我仅使用if"和else"语句使该程序运行,但是,我想使用以下方法创建相同的程序.当我运行程序时,我不断收到错误消息TypeError:create_purple() 需要 0 个位置参数,但给出了 2 个",我不知道为什么.

I have made this program work by using "if" and "else" statements only, however, I wanted to make this same program with the method below. When I run the program I keep getting an error "TypeError: create_purple() takes 0 positional arguments but 2 were given" and I cannot figure out why.

这是我的代码:

#*******************************************************************************************
#
# This program will take any two primary colors and create a secondary color.
#
# Primary colors are:
# Blue, Red, Yellow
# 
# Secondary colors are:
# Green, Orange, Purple
#

#Clear the screen
import os
os.system('cls') 

RED = "red"
BLUE = "blue"
YELLOW = "yellow"

def main():
    print ('\n')
    # Tell the user the objective of the program.
    print ('*****************************************************************************')
    print ('*****************************************************************************')
    print ('\n')
    print ('The objective of this program is to create a secondary color from two primary')
    print ('colors. When asked, choose two primary colors (Red, Blue, or Yellow) to create')
    print ('a secondary color ')

    # Choose your colors
    color1 = input('Enter your first primary color: ')
    color2 = input('Enter your second primary color: ')

    # Determine the secondary color
    if color1 == RED and color2 == BLUE:
    create_purple(color1,color2)


def create_purple():

    # Determine the color is purple
    if color1 == RED and color2 == BLUE:
        print ('\n')
        print ('You have made the color.... Purple ')
    else:
        if color1 == BLUE and color2 == RED:
            print ('\n')
            print ('You have made the color.... Purple ')

# Call the main function
main()

推荐答案

您使用 2 个参数调用 create_purple,但您的定义 (def) 有 0 个参数.

You are calling create_purple with 2 arguments, yet your definition (def) has 0 arguments.

只需更新定义:

def create_purple(color1,color2):

这篇关于类型错误:create_purple() 采用 0 个位置参数,但给出了 2 个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆