#can someone explain please

1 messages · Page 1 of 1 (latest)

steel zodiac
#

Im a little confused on what they are telling me to do

#

@tender hollow

sudden breach
#

Hm... can you say a little more about what you're confused about? I think it's pretty clear

steel zodiac
#

I’m new to coding so it’s hard for me to understand

sudden breach
#

See that formula that has c = and then an expression with a and b?
It's asking you to make a program that calculates c, if you give it a and b

#

Does that make sense what it is asking?

#

I can walk you through how to do it if you like

steel zodiac
#

Yeah it makes sense , yes that would be great

sudden breach
#

Okay, so the first step when you want to write a code, is to break it down into steps in English what you want the code to do.

#

The problem says "ask the user for a and b, and calculate the hypotenuse c"

#

So you would break this down into smaller steps like this:

  1. Ask the user for a
  2. Ask the user for b
  3. Calculate c
    And maybe
  4. Print c
#

Then, the next step is to take those steps in English, and write them as comments in your Python file, like this:

# Ask the user for a
# Ask the user for b
# Calculate c
# Print c

Now what you will do, is write code under each comment which does each thing.
So let's start with the first one: Ask the user for a.
Have you learned how to ask user for input and put it into a variable?

steel zodiac
#

So I would say , a=int(input(enter a))
And so on?

sudden breach
#

You are on the right track, but that is not quite the correct way to use input

#

If you want the program to say "Enter a:", you have to print this first

#

And then use input. Input doesn't take anything (in the parentheses), it is just used to get input

#

So you would write

#
print("Enter a")
a = int(input())
steel zodiac
#

Ohhhh , it’s starting to make a lot more sense now

sudden breach
#

So basically, first you print "Enter a" so that the user sees that text printed. Then the input() makes it wait for the user to enter something, and puts it into a

#
# Ask the user for a
print("Enter a")
a = int(input())

# Ask the user for b

# Calculate c
# Print c

So this is how you would fill in your program with asking the user for a. Can you try also filling in the next step, ask the user for b? Under the comment

rose mesa
#

You could also write a = input("Enter a: ")

sudden breach
#

Oh you can?

#

Sorry Aryan, I didn't know the python input function can take an argument. My bad.

rose mesa
#

Np haha

steel zodiac
#

I’m going to try to input it now and see what I get

sudden breach
#

@steel zodiac Yes so I was wrong above when I said it doesn't take any argument. You can write

a = int(input("Enter a"))
#

Just make sure you put it into quotes because it must be a string

#

So you cannot write input(enter a), the "enter a" has to be in quotes so that the program will consider it a string.

steel zodiac
#

Oh okay so I still put the # before I do that first right ?

sudden breach
#

The # is for writing a comment. The Python code will ignore everything you write on the line after #

#

Comments are not required, but are good practice as they are like little notes for yourself on what the code does

#

So you can write a comment
# Ask the user for a
It just tells you what the code does. Then on the next lines, write the code that does it.

steel zodiac
#

Ohh I get it

sudden breach
#

It's just a way of leaving a note to yourself.

steel zodiac
#

Okay Thankyou , I will let you know how the equation turns out. When I’m done

sudden breach
#

If you write this:
# print("hello")
It will not actually print anything, because the # makes it a comment, so it will ignore it (it thinks you are just writing as a note to yourself so it won't actually do the print)

#

Okay sure. The hardest part of the equation is doing the square root. For that, you just need to use the math.sqrt() function - look it up if you don't know it

steel zodiac
#

Line 1 and 2 works correctly it’s just line 3

#

What did I do wrong ?

#

They say this is what it’s supposed to look like

#

@sudden breach

sudden breach
#

You are printing c, but you never created a variable called c or did any calculation

steel zodiac
#

It didn’t provide me with any numbers

#

Do I create an equation?

sudden breach
#

You have gotten a and b from the user. You have to use a and b now to calculate c.

#

That's in the step
# calculate c

#

You will have to write something like...

# Calculate c
c = a + b

But instead of a + b, you have to do the equation for calculating the hypotenuse.

steel zodiac
#

So I would have to write it out as this?

sudden breach
#

You have to write that out using Python code.

#

The trick is knowing how to do squared, and square root.

#

To do square, you multiply the number by itself.
To do square root, use the math.sqrt function.

steel zodiac
#

How do I write squared root on keyboard

sudden breach
#

Python provides a function called math.sqrt that you have to use when you want square root

#

For example

#

math.sqrt(16) -> will give you 4

#

math.sqrt(25) -> will give you 5

#

As for square, like doing a squared, you have to multiply the number by itself, like this: a * a

steel zodiac
#

Ohh so I put c=math.sqrt a2+b2

#

?

sudden breach
#

No

#

To do the square, you have to multiply the number by itself

#

So instead of writing a^2, you need to write a * a

#

And to call math.sqrt, you have to give it whatever you want the square root of, inside the parentheses

#

math.sqrt(25) for example - you cannot write math.sqrt 25

#

it has to go in the parentheses

#

So, when you are writing code, you have to only use the operators that are valid in Python. There is no ^ in Python, so if you write ^ it won't mean anything in Python code.
Python does have an exponent operator, which is ** - so if you wanted to do, a^2, you can write a ** 2
But you can also just multiply it by itself - a * a

steel zodiac
#

Im confised on what to put after the second line , do I put (aa+bb)

sudden breach
#

You're doing good

#

You had something like this, right?
c = math.sqrt a^2 + b^2

steel zodiac
#

I had this

sudden breach
#

Good, so that's closer

#

So now what you're doing is

#

You're making c equal to a squared plus b squared

#

Does that make sense?

#

That's what the code you've written does. It does a times a, plus b times b, and puts that into c.

#

So you're making c = a squared plus b squared

#

Now, what you want is the square root of that

#

to take the square root, you have to use math.sqrt

#

and whatever you want the square root of, goes in the parentheses

#

So for example

#

if you wanted the square root of a + b, you would write math.sqrt(a + b)

#

if you want the square root of a * a + b * b, you would write... what?

steel zodiac
#

Math.sqrt (a * a + b* b)?

sudden breach
#

Perfect!

#

Yes, that is correct

#

So write that after your c =

#

But make sure you use lowercase math

#

not Math

#

in programming we have to be precise about things like uppercase and lowercase

steel zodiac
#

Okay I’m going to try

sudden breach
#

@steel zodiac By the way, once you have finished this line of code, you are still going to get the error in your screenshot. We can talk about that error. I just wanted to help you write the calculation for c first.

#

Oh, you need to write import math on its own line at the very top of your code

#

Sorry, I forgot to mention that

#

In order to use math functions, like math.sqrt, you have to import the math library.

#

After you've done that, there should just be one last thing to fix!

steel zodiac
#

It’s fine , so I write “import math “ on line 1 ?

sudden breach
#

Yes, on its own line at the very beginning of the program.

steel zodiac
#

Okay

sudden breach
#

Now you should get a different error

steel zodiac
#

Spaced or together

sudden breach
#
import math

Just like that

#

On its own line at the top

steel zodiac
#

Yeah it worked I got a different error this time

sudden breach
#

Good

steel zodiac
sudden breach
#

Now it's good to learn how to read error messages, because they will tell you what the problem is

#

I will teach you how to read this error message

steel zodiac
#

Okay Thankyou

sudden breach
#

The key parts of this error message are "can't multiply", and "non-int of type 'str'"

#

It's a little technical, but what it is essentially telling you, is that it cannot multiply something because it's not an int

#

You can only multiply numbers

#

But you are trying to multiply something of type 'str' - a string

#

Now, there is only one place in your program that you are doing multiplication. You are multiplying a times a, and b times b.

#

So this error message is basically saying: problem! a and b are strings, when they should be ints.

steel zodiac
#

Oh because they aren’t number ?

sudden breach
#

So that tells you what the problem is. When you wrote input(), you forgot to convert it into ints.

steel zodiac
#

Or that’s not the case

sudden breach
#

Remember at the start, you had written a = int(input())

#

You forgot the int when you changed it

#

Yes, it's an error because they aren't numbers

#

Whenever you get input, it is a string

#

So add the int() around it and it will be solved

steel zodiac
#

Ohh okay I will do that

#

Around a and b or just a

sudden breach
#

Both of them

#

a = int(input("Enter a"))

#

and same for b

#

It's both, because you want both to be ints.

steel zodiac
#

Oh okay , I have a problem though

#

It doesn’t tell me c

sudden breach
#

Yes, you never told it to tell you c

#

If you want the program to print c, what should you write?

steel zodiac
#

Print (c) ?

sudden breach
#

Yes

#

Try it

steel zodiac
#

It gave me an error

sudden breach
#

What does the error say?

#

And what exactly did you write to try and print c?

steel zodiac
sudden breach
#

name "math" is not defined means you forgot to import math

#

And indeed, I see the import math line is missing from your program

steel zodiac
#

Oh I accidentally deleted it

sudden breach
#

yup

steel zodiac
#

I forgot

sudden breach
#

so you will learn over time how to read error messages

#

when it's saying it does't know the name 'math', that means you forgot to import it

steel zodiac
#

Ohh okay

#

But now it’s says

Indentation error: unexpected indent

#

The line before that says

File “/home/main.py”, line 1
Import math

sudden breach
#

Did you indent it?

#

I will need to see your code to see what the problem is.

#

In Python code, indentation matters. So if you indented import math then it would be a problem.

steel zodiac
sudden breach
#

@steel zodiac The problem is you have a space on the first line

#

Before the word import

#

Make sure you don't have any spaces before it

steel zodiac
#

Ohh you were right when you said it was really precise

sudden breach
#

Well, the spaces thing is specific to Python

#

Because in Python, indentation actually has meaning in the code

#

So spaces in the wrong places is an error

#

In other programming languages it wouldn't matter

steel zodiac
#

Ohh okay

#

I’m going to remove the space once I’m done eating

#

And question, are you a teacher? because you explain things really well

sudden breach
#

Aww thanks. I'm not a professional teacher but I do like teaching

steel zodiac
#

I can tell , great trait

#

And it worked !

sudden breach
#

Good job!

steel zodiac
#

Thanks

#

@sudden breach i have another problem , i think i will be able to solve most of it on my own but i need some help starting, but it looks similar to the last problem though

sudden breach
#

I would encourage you to start the way we started last time

#

By writing down a step-by-step, in English, what you want the program to do

steel zodiac
#

okay i will make an attempt and let you know how it goes for me

#

but it doesnt have variables in this one though

#

I did this first part correctly I think @sudden breach

sudden breach
#

Good @steel zodiac - so the code you have written asks the user what they want.
But then, you should allow the user to enter the numbers, right? Once they have entered their numbers, you will need a variable to store what the user entered, so that you can use it in calculations later. Does that make sense?

steel zodiac
#

So I use int right ?

#

Yes it makes sense

sudden breach
#

Yes, you need to use input to get the user input, and int to convert it into a number, just like the other exercise.

steel zodiac
#

Okay I’m going to try it

#

But you know how we put a =int(input) ect…

sudden breach
#

The purpose of variables is to store values so you can use them later. So when you get the user input, put into a variable.

#

You can name the variable (almost) anything you want.

steel zodiac
#

This one doesn’t involve a b c

sudden breach
#

Yes, you can name your variables anything you want

#

You have the freedom in programming to make variables as you wish

#

For instance, maybe I want a variable named cat, and a variable named dog

#

I can do

cat = 10
dog = 20

Now cat is a variable with the value 10. And dog is a variable with the value 20.

#

It's nonsensical but my point is you can name a variable anything you want. You should try to pick a name which makes more sense, though!

steel zodiac
#

Oh okay , so I’m going to use pesos , soles , and reais

sudden breach
#

Good idea

#

Ok @steel zodiac I have to go for a while, but please take my advice - the best advice I have to give, is learn how to think about your code in English. When you write code, describe what it does in English. For example:
print("what do you have left in pesos?")
In English: Print the text "what do you have left in pesos?" to the screen.
That's all it does - it prints the text. It does not ask for input, it does not calculate anything, it just prints the text.
Another example:
a = int(input("Enter a"))
In English: Print "Enter a", wait for user to enter input, convert the input to an int, and store that value in the variable a.
This may seem tedious, but this skill will serve you well. It will help you understand exactly what your code is doing.
Finally, do your best to try to understand the error messages you get, and pay attention to things like spaces, spelling, uppercase/lowercase as code has to be written very precisely or it will complain.

steel zodiac
#

It’s making a lot more senseee

#

Almost done also , but okay see ya later