【问题标题】:TypeError: create_purple() takes 0 positional arguments but 2 were givenTypeError: create_purple() 接受 0 个位置参数,但给出了 2 个
【发布时间】:2014-11-26 11:03:02
【问题描述】:

我是 Python 编程的新手,我正在努力使这个程序工作。

我只使用“if”和“else”语句使这个程序工作,但是,我想用下面的方法制作这个程序。当我运行程序时,我不断收到错误“TypeError:create_purple() 采用 0 个位置参数,但给出了 2 个”,我不知道为什么。

这是我的代码:

#*******************************************************************************************
#
# 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()

【问题讨论】:

  • 我讨厌这样做 - 我在发布问题后就想通了。我需要在 def create_purple(): 行中插入“color1,color2”。它应该是... def create_purple(color1,color2):

标签: python class python-3.x arguments typeerror


【解决方案1】:

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

只需更新定义:

def create_purple(color1,color2):

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-13
    • 2021-02-02
    • 2013-09-23
    • 2017-10-05
    • 2016-01-30
    • 2019-06-26
    • 2014-11-12
    • 2018-09-20
    相关资源
    最近更新 更多