Programming help
what is this?
@vocaloid
im not good at programming, heck i barely passed pltw in 7th and 8th grade
@tranquility
the way you have it set up, the calculations are already written out for s = 5, but the point of this exercise is to let the user decide what the dimensions are and have the program calculate it for them if you're doing the cube, then you need to ask the user what the side length is, save that as the input, then functions that take the user input and calculate the volume and surface area (you will have to write the given volume and surface area equations in the program)
the basic logic: side = input("What is the side length of your cube?") volume = side ** 3 print("The volume is: ", volume) taking it a step further, you have to make sure the input is converted to a float, and also handles the exception if the user tries to enter something that isn't a number
the idea of the code is that the user inputs what side length they want and the program calculates the volume and surface area so you need to ask the user what they want the side length to be, and have the program user that side length to do the calculations
ohh ok
side = input("What is the side length of your cube?") volume = side ** 3 print("The volume is: ", volume) so where would i add this to my code?
I would personally start over from scratch in the code, explain to the user the scenario of the problem (John wants to order a rubick's cube, and he wants it to fit on the shelf, etc...) then prompt the user for the side length, then calculate the volume, then print the volume then write something similar for the surface area calculation
wait
do i ask for the side length on that part of the code
or
this part
almost, the user will be providing the side length, not the volume and SA. the program will be calculating those.
ok so only ask for the side length and remove the volume and sa
and i will add the volume and sa at the end of the whole program
the side length is the main question in this code right
yes
let's look at this again: side = input("What is the side length of your cube?") volume = side ** 3 print("The volume is: ", volume) notice the second line is the volume function V = S^3, written as code. how would we write one for surface area? remember that * means multiply and ** means an exponent.
also that surface area = 6s^2
this is what i have done so far, am i doing good?
good so far, now you need to do the surface area and it'll be done
actually I'm pretty sure the input function already displays the string on the screen so I don't think you need to repeat the string in the print statement
i think i tried that but the word problem wasnt popping up for the user to read, instead it would only show on the output part of the program if that makes sense..
oh ok then
i want it to show like that
you can leave the repeated strings then
i have a problem with one line
this is line 9
by default input converts to a string, so you need to convert the input to an int or a float (float is probably better if you want the program to be able to handle non integers) you can modify the input by writing side = int(input("Prompt")) or float(input("Prompt"))
but is this supposed to happen?
no, you fill in the prompt with the original prompt (asking for side length)
I was just using prompt as a placeholder
OHH
so replace it with
What is the side length of the cube?
yeah
what does this mean?
i typed in "random" when it asked me the side length
line 8: side = int(input("What is the side length of the cube?"))
you have it set up so that it will convert the input to an integer, but you cannot convert the word "random" to a number. if you want to be able to handle a situation where a user enters a non-number, you need to set up a case where the program will tell the user something like "You must enter a number" and repeat the prompt. Not sure if you've learned about exception handling, your teacher may not be expecting you to be able to program that yet
so what can i enter for it to work
right now
okay let me try
the input has to be a number
okay it worked but
it says theres another error on line 9 which is "print("The volume is: ", volume)"
error: NameError: name 'volume' is not defined on line 9
you need to define the volume variable as volume = side ** 3 before printing volume
okay
like this?
i ran the program and it seems to work fine now
good, be sure to add a surface area calculator (following the same logic as the volume calculator) and you should be set
Okay, i will add that now
works perfectly
its just that is it okay to have "surfacearea" together like that becuase i cant space it like "surface area" i really dont think this matters tho
yes, variables have to be one word, it'll throw a syntax error if you try to make a variable with a space in the name however, keep in mind the surface area is not s^3, it's 6s^2 which would be 6 * side ** 2
like this?
looks alright to me, do you want to try running it one last time
yes but theres one more thing
I'm not sure what you mean, are you talking about the extra info in paragraph 3?
actually hold on
yes, that last paragraph is showing the output after the user has input all their data if you want to print out your side length at the end, you simply need to include a line that says print("A cube with side length ", side, "has volume," volume, " and surface area," surface area") or something like that
where do you think i can add that piece of code?
before volume = side ** 3?
the function you've already written already has the volume and surface area printed at the end, I don't think it's necessary to re-state the radius
oh alright thats fine then
ill just leave it as it is
ill just run this one last time
nice if you really wanted to re-state the radius, you could simply modify the print statements like so print("The volume of a cube with side length ", side, "is: ", volume) and similarly for SA, but I think it's fine as is
Yes, this is perfect for me. thank you so much. I would have been so lost without your help lol.
Join our real-time social learning platform and learn together with your friends!