Python Forum

Full Version: beginner math
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have a problem to solve and my understanding is poor. The answer to my problem is 12 and I answered 9. Can someone explain to me, please
how the math was done? Sorry for my newbie question


def hi (a, b)
	return a * b

def hello (a, b)
	return hi(a, b + 1)

hello(4, 2)
9 answer 12
first of all this code will not work because you miss colon at the first line of each function. After fixing it and adding print to see the result I get what's expected
def hi (a, b):
    return a * b
 
def hello (a, b):
    return hi(a, b + 1)
 
print(hello(4, 2))
Output:
12
thank you for the answer but can you explain to me how the was done? I have a couple more problems similar and I have to do them without any compiler, how do I explain the answer is 12. Sorry for insisting
Trace through the execution of the functions by hand. They aren't that complicated, so it shouldn't be hard.

Also, why do your functions have such poor names? If you're being taught to name them like that, it's not a good thing.
I am in 6th grade, I understand hard from my teacher he talks too fast. I really want to know this. From my understanding a= 4 and b= 2
that means 4*2=8
so 'hi' is =8
same for 'hello' but when returns it calls 'hi' which means
8(4,2+1)

but why the result is 12? Cry
sorry, mister Buran, I see that I have offended you with my 6th-grade math. Yes, it was one of my homework problems but because I have no explanation I won't be able to do them. Is not all about running it in a compiler, I have to explain it too and this is all the code I was given and not a complete one like in your comment.
def hi (a, b)
	return a * b

def hello (a, b)
	return hi(a, b + 1)

hello(4, 2)
9 answer 12.
Thanks for being rude to me in private. If you think that helping me with a problem you do my homework then I asked for help on a wrong site.
You were perfectly right about "If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein"
There are a few problems with the code. As mentioned before, the code as posted cannot run. I copied your code and when I try to run it Python complains about "Invalid Syntax" and highlights the end of line 1. I know functions need a colon, so I added the colon and tried again.
def hi (a, b):
return a * b

def hello (a, b):
return hi(a, b + 1)

hello(4, 2)
Now Python complains about "expected an indented block" and highlights the start of line 2. I know the code inside a function needs to be indented, and I also know a lot of time code is posted without surrounding the code with python tags so I indented the code and tried again.
def hi (a, b):
    return a * b

def hello (a, b):
    return hi(a, b + 1)

hello(4, 2)
Now I run the code and I don't get any errors, but I also don't get any output. I added a print statement to see what hello() returns.
def hi (a, b):
    return a * b

def hello (a, b):
    return hi(a, b + 1)

print(hello(4, 2))
This program prints 12 when run. This makes me think you and I are not running the same code. Your code not having a print statement is also suspicious. Are you typing in Python commands at the '>>>' prompt or are you running a Python program saved in a file. I decide to see what happens if I type you code in at the Python '>>>' prompt.
Output:
>>> def hi(a, b): return a*b >>> def hello(a, b): return hi(a, b+1) >>> hello(4, 2) 12
Hmmm. I am getting 12 back when I call hello(4, 2). When you look at what I tried, how does it differ from what you are doing?
(Nov-29-2020, 10:50 PM)MarioM Wrote: [ -> ]Thanks for being rude to me in private.
No one was rude to you. And by the way, moderator note was my mistake. I wanted to post different one - about using BBcode tags when post code, error, etc. The only reason was that at the time I didn't realise it is a homework.
We expect that you be frank and post homework questions in the respective section. I moved it to Homework section now.
Do you understand how functions work? Look at your study material. This site may help visualise the execution http://www.pythontutor.com/visualize.html#mode=edit and get better understanding of the code execution.
hi dean,
thank you for your patience

http://silentteacher.toxicode.fr/hour_of...sic_python

this is the page where I have been instructed to do my homework. I have to take print screens and show them to my teacher and explain how I have done it but I am at a stage where even if somebody will give me the right answer I won't be able to explain it so I must understand how math was done
(Nov-30-2020, 07:11 AM)MarioM Wrote: [ -> ]I won't be able to explain it so I must understand how math was done
that's why I give you a link to a site where you will execute the code and see how it works and then you can explain in your own words.
as suggested by @ndc85430 - you can trace the execution by hand. Of course you need really basic understanding of how the functions work.
No one will write the full explanation for you.

Note, as already mentioned - the code you provide has a syntax errors (missing colons) and will not work before these errors are fixed. We don't know if this is done by purpose or by mistake.
Pages: 1 2