Home > Mobile >  Just started using Python 3 and no matter what I do I can't seem to get exponentials to work. A
Just started using Python 3 and no matter what I do I can't seem to get exponentials to work. A

Time:07-28

As the title says, I've tried all kinds of things. Essentially I'm trying to write a function that accepts an integer as an argument and returns its square.

This is an example of what I've tried on my own:


number1 = "2"

def square_number(number1):
    return("number1" ** 2)

This didn't work but I've tried a copy past deal from the internet to check:


number1 = 5

square1 = pow(number1, 2)

This also didn't work. All I see is this:


======================== RESTART: /Users/NK/Documents/Python training/Learning functions.py ========================

>>>

just a blank line.

CodePudding user response:

You are not instructing python to print anything to the screen.

In the first case, you are simply defining a function. More on this later *

In the copy-pasted code, you are squaring number1, but then do nothing with the result. If you want to show it, you need to instruct it:

print(square1)

*

Back to your trial in your trial, there are a few errors. First, you should put a number in the variable number1. Currently, because you've put 2 inside quotes "2", your variable is a string, not a number. Instead, say number1 = 2 (no quotes).

Second, in your function, you are using quotes around number1. Then this is also a string, so you're not referencing your variable, but you are squaring the string "number1" . This will not work and will throw an error.

Currently nothing is happening because you have only defined what the function should do. You haven't used the function yet.

Note that the parameter of the function can have any name, so you can define it as:

def square_number(a): return a ** 2

Here you've just define what the function should do when called. It is independedn to anything outside of it, like number1.

Then you can call your function, with a parameter:

sq3 = square_number(3)
sq4 = square_number(4)
sq_n1 = square_number(number1)

and then you can print the results, which here I've put in variables

print(sq3, sq4, sq_n1)

Again, note, no quotes, I'm referring to the variables.

CodePudding user response:

I would suggest you start first by:

  1. Reading about python coding convention, for the readability of your code.
  2. Learn how to run the interpreter in your terminal.
  3. Learn how to execute code in a .py file.

Usually, you will write your code in a .py file. But it is a good practice to always try to run every line of your code first at the terminal to know what it will change.

Terminal example

Part of your script squares a number, so you go to your terminal and experiment with the different ways of how can you do squaring in python.

ziadh@Ziads-MacBook-Air ~ % python3
Python 3.10.4 (v3.10.4:9d38120e33, Mar 23 2022, 17:29:05) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> # first method using pow
>>> num_one: int = 3
>>> pow(num_one, 2)
9
>>> pow(4, 2)
16
>>> # second method using **
>>> 4**2
16

Now you are an expert in squaring numbers in python, you go to your .py file and write the following.

num_one: int = 3
num_one_squared: int = pow(num_one, 2)
print(num_one_squared)
# you can also do
num_two: int = 4
num_two_squared: int = num_two**2
print(num_two_squared)
  • Related