简体   繁体   中英

Python Parameter passing for another function

I have created two functions namely inputData(): and validateNumber(): In the inputData() function I enter a value and stores it in a variable called number. And then I want to pass that parameter to validateNumber(): function. But it isn't work :( It would be fine if anyone explain me the error :) Regards.

Here's the code:

def inputData(): 
    number = int(input("Enter a Number: ")) 
    print(number)
    return number 

def validateNumber(number): 
    n=2 
    while number > n:
        if number%n==0 and n!=number:
            print("Not Prime") 
            break 
        else: 
            print("Prime") 
            break 
    return number

inputData()
validateNumber()

这是错误代码

You need to perform the function call as follows:

validateNumber(inputData())

or

number = inputData()
validateNumber(number)

with def validateNumber(number) you are telling python that the function validateNumber must receive one parameter when it is called. But, you are not passing the parameter to it when you call it.

If you are new to programming, check this tutorial: Python Functions , to understand:

  • What are functions
  • How to define them
  • How to use them.

You need to store the value of inputData() function in some variable then pass it to second function like this

>> number = inputData()
>> validateNumber(number)

You're not passing the inputted number to the validate function.

returned_input_number = inputData()
validateNumber(returned_input_number)

Also, I find it a bit odd that your validateNumber function returns a number. It might be better to return True or False (depending on if the number is valid or not). Either that, or maybe 'validate' is the wrong name for the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM