简体   繁体   中英

Python program with def function

My goal is as follows:

  • Define a function called elements() with no arguments.
  • Prompt user to input three favorite elements.
  • Assign input to a variable called ele .
  • Return the variable ele .
  • Save the output of this function to a variable called s_elements .
  • Print "So your favorite chemical elements are Na, K, Mg" .

So far, I have

def elements():
       ele = input("What are your three favorite chemical elements? ")
       s_elements=print ele
       print("So your favorite chemical elements are", s_elements)

I'm getting an error with print ele and the function is not working properly. How can I fix this?

You are not following the instructions of your assignment properly.

You are supposed to return ele . So first:

def elements():
       ele = input("What are your three favorite chemical elements? ")
       return ele

Then, your assignment states you need to call the method and store it in s_elements :

s_elements = elements()

Then you are supposed to print it, which that part you have done properly:

print("So your favorite chemical elements are", s_elements)

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