function at the end of code- why do i have to place it?

2024/10/5 15:06:07

I'm just starting my adventure with Python. Unfortunately, I can't figure out why, at the end of my code, I have to add myfunc(). Without it my code doesn't display.

How is it if I use more than one definition then I have to put each definition at the end of the code?

def myfunc(a=4,b=6):sum = a + bprint(sum)myfunc()
Answer

The thing throwing you off is that you don't need to put all your code into a function. Your code could (and should) be rewritten as this:

sum = a + b
print(sum)

This code will do the exact same thing. A function, by definition, is a block of code that is given a name, so you can use it multiple times without having to rewrite the whole block of code every time you want to use it. Because you are putting your code into a function, you must tell Python that you want to run that code too.

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

Related Q&A

Would like to write code in idle that I have in Jupyter notebook. It is using Timeit

code big_array= np.random.rand(1000000) %timeit sum(big_array) this code above is done in jupyter notebook how do I use this code in idle

How to pass python variable to shell command [duplicate]

This question already has answers here:How to escape os.system() calls?(10 answers)Closed 2 years ago.Is their any way that i can pass variable value of python to my unix command Note : var is a pytho…

A function inside a function Python

This is a problem asked before but I cant really understand the other explain of this kind of problem so Im here to re-write it in more details. While studying I have encountered this kind of code tha…

Accessing a parameter passed to one function in another function

I have two functions:def f1(p1=raw_input("enter data")):...do somethingdef f2(p2=raw_input("enter data")):...do something elsep1 and p2 are the same data, so I want to avoid asking …

SOLVE: AttributeError: ImmutableDenseNDimArray object has no attribute as_independent

I am quite new to python and I have been trying to plot this in a few different ways. If I try using np.vectorize, it crashes. so i wrote this code, which is giving me the error in the title: import ma…

Python memory limit [duplicate]

This question already has answers here:In-memory size of a Python structure(7 answers)Closed 10 years ago.So clearly there cannot be unlimited memory in Python. I am writing a script that creates lists…

Automating Gmail login in Python

I am writing a Python program that can login Gmail.The purpose of this program is to check whether the username/password combination exists and is correct.Since this program is to test the the username…

Reversing order in incrementing digits

I have a list of numbers, and Im trying to do the following in a way as efficient as possible.For each consecutively incrementing chunk in the list I have to reverse its order.This is my attempt so far…

why python selenium get the empty page_source?

I try to simulate the buy item operation on the link below. (Need login in first)taobao_item_linkAnd after you click the button below. img_link:The link will jump to a new link.But if I print out the p…

How do i print the repetition output using regex it prints only first match

I have task where I need to print the occurrence and count and non-occurrence and count import resequence = 1222311m = re.search(r(\d)\1+,sequence)print(m) Exptected output : (1, 1) (3, 2) (1, 3) (2…