Ask your own question, for FREE!
Computer Science 14 Online
Gucchi:

Programming help

RenegadeMaster:

what is this?

Gucchi:

@vocaloid

RenegadeMaster:

im not good at programming, heck i barely passed pltw in 7th and 8th grade

Gucchi:

@tranquility

Vocaloid:

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)

Vocaloid:

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

Vocaloid:

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

Gucchi:

ohh ok

Gucchi:

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?

Vocaloid:

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

Gucchi:

wait

Gucchi:

do i ask for the side length on that part of the code

Gucchi:

or

Gucchi:

this part

Vocaloid:

almost, the user will be providing the side length, not the volume and SA. the program will be calculating those.

Gucchi:

ok so only ask for the side length and remove the volume and sa

Gucchi:

and i will add the volume and sa at the end of the whole program

Gucchi:

the side length is the main question in this code right

Vocaloid:

yes

Vocaloid:

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.

Vocaloid:

also that surface area = 6s^2

Gucchi:

this is what i have done so far, am i doing good?

Vocaloid:

good so far, now you need to do the surface area and it'll be done

Vocaloid:

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

Gucchi:

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..

Vocaloid:

oh ok then

Gucchi:

1 attachment
Gucchi:

i want it to show like that

Vocaloid:

you can leave the repeated strings then

Gucchi:

i have a problem with one line

Gucchi:

1 attachment
Gucchi:

1 attachment
Gucchi:

this is line 9

Vocaloid:

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"))

Gucchi:

but is this supposed to happen?

1 attachment
Vocaloid:

no, you fill in the prompt with the original prompt (asking for side length)

Vocaloid:

I was just using prompt as a placeholder

Gucchi:

OHH

Gucchi:

so replace it with

Gucchi:

What is the side length of the cube?

Vocaloid:

yeah

Gucchi:

1 attachment
Gucchi:

what does this mean?

Gucchi:

i typed in "random" when it asked me the side length

Gucchi:

line 8: side = int(input("What is the side length of the cube?"))

Vocaloid:

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

Gucchi:

so what can i enter for it to work

Gucchi:

right now

Gucchi:

okay let me try

Vocaloid:

the input has to be a number

Gucchi:

okay it worked but

Gucchi:

it says theres another error on line 9 which is "print("The volume is: ", volume)"

Gucchi:

error: NameError: name 'volume' is not defined on line 9

Vocaloid:

you need to define the volume variable as volume = side ** 3 before printing volume

Gucchi:

okay

Gucchi:

like this?

Gucchi:

i ran the program and it seems to work fine now

Vocaloid:

good, be sure to add a surface area calculator (following the same logic as the volume calculator) and you should be set

Gucchi:

Okay, i will add that now

Gucchi:

works perfectly

Gucchi:

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

Vocaloid:

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

Gucchi:

like this?

Vocaloid:

looks alright to me, do you want to try running it one last time

Gucchi:

yes but theres one more thing

Vocaloid:

I'm not sure what you mean, are you talking about the extra info in paragraph 3?

Vocaloid:

actually hold on

Vocaloid:

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

Gucchi:

where do you think i can add that piece of code?

Gucchi:

before volume = side ** 3?

Vocaloid:

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

Gucchi:

oh alright thats fine then

Gucchi:

ill just leave it as it is

Gucchi:

ill just run this one last time

Vocaloid:

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

Gucchi:

Yes, this is perfect for me. thank you so much. I would have been so lost without your help lol.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!