#can someone explain please
1 messages · Page 1 of 1 (latest)
Hm... can you say a little more about what you're confused about? I think it's pretty clear
I’m new to coding so it’s hard for me to understand
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
Yeah it makes sense , yes that would be great
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:
- Ask the user for a
- Ask the user for b
- Calculate c
And maybe - 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?
So I would say , a=int(input(enter a))
And so on?
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())
Ohhhh , it’s starting to make a lot more sense now
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
You could also write a = input("Enter a: ")
Oh you can?
Sorry Aryan, I didn't know the python input function can take an argument. My bad.
Np haha
I’m going to try to input it now and see what I get
@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.
Oh okay so I still put the # before I do that first right ?
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.
Ohh I get it
It's just a way of leaving a note to yourself.
Okay Thankyou , I will let you know how the equation turns out. When I’m done
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
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
You are printing c, but you never created a variable called c or did any calculation
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.
So I would have to write it out as this?
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.
How do I write squared root on keyboard
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
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
Im confised on what to put after the second line , do I put (aa+bb)
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?
Math.sqrt (a * a + b* b)?
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
Okay I’m going to try
Okay gotcha
@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!
It’s fine , so I write “import math “ on line 1 ?
Yes, on its own line at the very beginning of the program.
Okay
Now you should get a different error
Spaced or together
Yeah it worked I got a different error this time
Good
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
Okay Thankyou
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.
Oh because they aren’t number ?
So that tells you what the problem is. When you wrote input(), you forgot to convert it into ints.
Or that’s not the case
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
Both of them
a = int(input("Enter a"))
and same for b
It's both, because you want both to be ints.
Yes, you never told it to tell you c
If you want the program to print c, what should you write?
Print (c) ?
It gave me an error
name "math" is not defined means you forgot to import math
And indeed, I see the import math line is missing from your program
Oh I accidentally deleted it
yup
I forgot
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
Ohh okay
But now it’s says
Indentation error: unexpected indent
The line before that says
File “/home/main.py”, line 1
Import math
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 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
Ohh you were right when you said it was really precise
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
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
Aww thanks. I'm not a professional teacher but I do like teaching
Good job!
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
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
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
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?
Yes, you need to use input to get the user input, and int to convert it into a number, just like the other exercise.
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.
This one doesn’t involve a b c
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!
Oh okay , so I’m going to use pesos , soles , and reais
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.