Python Coding (call function / return values)

2024/10/6 12:28:29

I am having trouble writing code for this question. I have seen this question asked in a few places but I still cannot figure out the answer from the tips they provided.

The question is: Write a program that has two functions: first() and second(). Function first() should print the string "In function first()" and then call function second(). Function second() should print the string "In function second(). In the global scopre, you should call function first().

This is the code I have..

def first():first = "In function first"def second():second = "In function second"print first(), second()

Does this look any closer? It still doesn't work but I put the print functions back in..

Answer

first = "In function first" doesn't make the function return "In function first".

I can understand why you might think otherwise. In some languages, the way to return a value is to assign it to the function. In other languages, assignment is an expression, and the last expression evaluated inside a function is the return value. So, there are a lot of languages where what you're doing would work.

But Python isn't one of them. In Python, the only way to return a value is with the return statement. And if you don't use a return statement, the caller just gets None.

So, your code just creates a local variable with (confusingly) the same name as the function, assigns a value to that, and then ignores that variable, so it ultimately has no effect.

What you want is:

def first():return "In function first"def second():return "In function section"print first(), second()

However, while that will give you the desired output, it isn't actually doing what you said you wanted:

Function first() should print the string "In function first()"

In that case, it should have a print in it, not return a value that someone else has to print.

… and then call function second().

Then you need to put the call to second() inside the definition of first, not outside of it.

Function second() should print the string "In function second().

And again, it should print, not return.

So, this is a much better match to what you were trying to do:

def first():print "In function first"second()def second():print "In function second"first()

That being said, the code you wrote (with this fix) actually seems like better code than the code you wanted to write. Returning values from inner functions and handling output at the highest level is generally more flexible than having prints all over the place.

https://en.xdnf.cn/q/119428.html

Related Q&A

discord.py Mention user by name

I am trying to mention a user by their name in discord.py. My current code is: @bot.command(name=mention) @commands.has_role(OwnerCommands) async def mention(ctx, *, member: discord.Member):memberid = …

Python unexpected EOF while parsing : syntax error

I am trying to do a simple toto history with a dictionary and function however I have this funny syntax error that keeps appearing that states "unexpected EOF while parsing" on the python she…

How can I read part of file and write the rest to another file?

I have multiple large csv file. How can I read part of each file and write 10% of the data/rows to another file?

Encryption/Decryption - Python GCSE [duplicate]

This question already has an answer here:Encryption and Decryption within the alphabet - Python GCSE(1 answer)Closed 8 years ago.I am currently trying to write a program, for school, in order to encryp…

Convert decimal to binary (Python) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Kivy Digital Clock Issues

Im trying to add a digital clock to my Kivy program, it seems to be having trouble.Here is the .py:import kivykivy.require(1.10.0)from kivy.lang import Builder from kivy.uix.screenmanager import Screen…

Read a file name and create a column with it [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 4…

Python : T test ind looping over columns of df

My dataframe is composed of accounting variables and a dummy variable that allows me to identify two types of company. I would like to perform a t-test for every column of my dataframe in order to comp…

I want to understand which line of code outputs **none** in the function

The last line of the output is none can someone explain why pls def just_lyrics():print ("i am a bad coder")print (" i keep trying to learn everday")def double_lyrics():just_lyrics(…

How to clear python console (i.e. Ctrl+L command line equivalent)

OS = Linux[boris@E7440-DELL ~]$ uname -a Linux E7440-DELL 3.17.4-200.fc20.x86_64 #1 SMP Fri Nov 21 23:26:41 UTC 2014 x86_64 x86_64 x86_64 GNU/LinuxFrom python console (Spyder 2.2.4, Python 2.7.5 64bits…