Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming homework (Python) - Help!
New Posts  All Forums:Forum Nav:

# Programming homework (Python) - Help!

Never programmed a day in my life, can you guys help me. You don't have to solve it all, I just need some direction.
Quote:
Program Work 40 points
Given:

The superhero known as the UberGeek is trapped in a small room inside of a saltmine. To escape, the geek must cut
a 1 meter cylinder through 25 meters of salt using a high-power laser. The result is a cylindrical volume of salt that
has to be moved.

Height= 25 meter
Area of a circle = pi(r)^2
Volume of a cylinder= Area of the base * height
1,000,000 milliliters are in one cubic meter.
Density of salt is: 2.15 grams per milliliter
1000 grams in one Kilogram.
Use import math to be able to access the variable math.pi for the value of pi

Find :
Step 1: Create a variable for the radius of the circle.
Step 2: Create a variable for the height of the cylinder
Step 3: Create a variable for the area of the base in square meters and apply the equation.
Step 4: Create a variable for the volume of the cylinder in cubic meters and apply the equation.
Step 5: Create a variable for the volume of the cylinder in milliliters and apply the conversion
Step 6: Create a variable for the mass of the cylinder in grams by applying the density.
Step 7: Create a variable for the mass of the cylinder in kilograms and apply the conversion.
 Po' Pimpin' (11 items)
CPUMotherboardRAMHard Drive
i5 2500k @ stock Biostar TZ68K+ [A3] 4GB  Sandforce 1222 64GB SSD
Optical DriveCoolingOSMonitor
LG 22x DVD-+RW  Stock Windows 7 x64 Acer S211HL 1080p
PowerCaseMouse
 Po' Pimpin' (11 items)
CPUMotherboardRAMHard Drive
i5 2500k @ stock Biostar TZ68K+ [A3] 4GB  Sandforce 1222 64GB SSD
Optical DriveCoolingOSMonitor
LG 22x DVD-+RW  Stock Windows 7 x64 Acer S211HL 1080p
PowerCaseMouse
So you just need to do as the steps say. Not too hard, just a little algebra. Plus all the information is already given for you, just needs to be put in the correct fields Just make sure you either convert your measurements to the same thing like millimeter to meter, or do some other thing that makes it do that. And don't forget to call your math module! Simple google will tell you how to do so.

so for the first variable for example:
Code:
````cirradius = 1 #this is the radius of the circle`
```

and the second being
Code:
````cylheight = 25 #this is the height of the cylinder`
```

That should give you a decent start. Just follow the steps 1 by 1, just like a computer would and you're golden!

Good luck!
 Skyship (13 items)
CPUMotherboardGraphicsRAM
Phenom II 1090T BE ASUS Crosshair IV XFX 5770 OCZ AMD Edition
Hard DriveOSMonitorKeyboard
Seagate Windows 7 Pro Sceptre 22" widescreen Saitek Eclipse 1
XION 800w modular Cooler Master Storm Scout Razer DeathAdder Steel Series QCK
 Skyship (13 items)
CPUMotherboardGraphicsRAM
Phenom II 1090T BE ASUS Crosshair IV XFX 5770 OCZ AMD Edition
Hard DriveOSMonitorKeyboard
Seagate Windows 7 Pro Sceptre 22" widescreen Saitek Eclipse 1
XION 800w modular Cooler Master Storm Scout Razer DeathAdder Steel Series QCK
If you want to cheat this is the easy way out (Click to show)
Code:
``````#get pi from math lib
from math import pi
#definitions
Height = 25
VolumeOfCylinder = AreaOfCircle * Height
VolumeOfCylinderMillimeters = VolumeOfCylinder * 1000000
MassOfCylinder = 2.15*VolumeOfCylinder
MassOfCylinderKilograms = MassOfCylinder/1000
#spit it out
print ('Height is: %s meters' % Height)
print ('Area of circle is: %s meters squared' % AreaOfCircle)
print ('Volume of cylinder is: %s cubic meters ' %  VolumeOfCylinder)
print ('Volume of cylinder in millimters is: %s millimeters cubed' % VolumeOfCylinderMillimeters)
print ('Mass of Cylinder is: %s grams' % MassOfCylinder)
print ('Mass of cylinder in kilograms is: %s kilograms' % MassOfCylinderKilograms)```
```

Things to note:
you import pi by using:
Code:
````from math import pi`
```

To do a exponent in this case you can use the ** operator:
Code:
````nine = 3 ** 2`
```

Defining a variable:
Python is not strongly typed so you do not have to define what the variable is(a number or a string) before you use it. Because of this you can simply assign a value to a variable and manipulate it. Also you can reference variables in other variables. For example:
Code:
``````Nine = 3 ** 2
Eighteen = Nine * 2
Carrot = "carrot"

print ("Nine %s" %Nine)
print ("Eighteen %s" % Eighteen)
print ("Carrot: %s" %Carrot)```
```
Would print:
Nine 9
Eighteen 18
Carrot: Carrot
Edited by HalfWayCrook - 1/28/12 at 11:16pm
Quote:
Originally Posted by HalfWayCrook

If you want to cheat this is the easy way out (Click to show)
Code:
``````#get pi from math lib
from math import pi
#definitions
Height = 25
VolumeOfCylinder = AreaOfCircle * Height
VolumeOfCylinderMillimeters = VolumeOfCylinder * 1000000
MassOfCylinder = 2.15*VolumeOfCylinder
MassOfCylinderKilograms = MassOfCylinder/1000
#spit it out
print ('Height is: %s meters' % Height)
print ('Area of circle is: %s meters squared' % AreaOfCircle)
print ('Volume of cylinder is: %s cubic meters ' %  VolumeOfCylinder)
print ('Volume of cylinder in millimters is: %s millimeters cubed' % VolumeOfCylinderMillimeters)
print ('Mass of Cylinder is: %s grams' % MassOfCylinder)
print ('Mass of cylinder in kilograms is: %s kilograms' % MassOfCylinderKilograms)```
```
Things to note:
you import pi by using:
Code:
````from math import pi`
```
To do a exponent in this case you can use the ** operator:
Code:
````nine = 3 ** 2`
```
Defining a variable:
Python is not strongly typed so you do not have to define what the variable is(a number or a string) before you use it. Because of this you can simply assign a value to a variable and manipulate it. Also you can reference variables in other variables. For example:
Code:
``````Nine = 3 ** 2
Eighteen = Nine * 2
Carrot = "carrot"
print ("Nine %s" %Nine)
print ("Eighteen %s" % Eighteen)
print ("Carrot: %s" %Carrot)```
```
Would print:
Nine 9
Eighteen 18
Carrot: Carrot

Ahh! I think I understand.

So would Nine be:
Quote:
Nine == 3*3 or 3**2
Quote:
Originally Posted by Poseiden

So you just need to do as the steps say. Not too hard, just a little algebra. Plus all the information is already given for you, just needs to be put in the correct fields Just make sure you either convert your measurements to the same thing like millimeter to meter, or do some other thing that makes it do that. And don't forget to call your math module! Simple google will tell you how to do so.
so for the first variable for example:
Code:
````cirradius = 1 #this is the radius of the circle`
```
and the second being
Code:
````cylheight = 25 #this is the height of the cylinder`
```
That should give you a decent start. Just follow the steps 1 by 1, just like a computer would and you're golden!
Good luck!

Did you define "cirradius" and "cylheight" or were they already defined in Python?
 Po' Pimpin' (11 items)
CPUMotherboardRAMHard Drive
i5 2500k @ stock Biostar TZ68K+ [A3] 4GB  Sandforce 1222 64GB SSD
Optical DriveCoolingOSMonitor
LG 22x DVD-+RW  Stock Windows 7 x64 Acer S211HL 1080p
PowerCaseMouse
 Po' Pimpin' (11 items)
CPUMotherboardRAMHard Drive
i5 2500k @ stock Biostar TZ68K+ [A3] 4GB  Sandforce 1222 64GB SSD
Optical DriveCoolingOSMonitor
LG 22x DVD-+RW  Stock Windows 7 x64 Acer S211HL 1080p
PowerCaseMouse
Quote:
Originally Posted by BizzareRide

Did you define "cirradius" and "cylheight" or were they already defined in Python?

He just defined them. When you type:
something = whatever

You're setting the variable 'something' equal to the element 'whatever'.

See here for a quick basic introduction to python.
It's pretty good, and should get you understanding more in no time!

http://hetland.org/writing/instant-python.html
 My System (16 items)
CPUMotherboardGraphicsRAM
i7 5820K @4.4ghz Asus X99-S EVGA GTX680 2GB 32GB Crucial 2133MHz DDR4
Hard DriveCoolingOSMonitor
120GB Samsung 830 | 120GB Samsung 840 EVO | 6TB... Noctua NH-D14 Windows 8.1 Achieva Shimian 27" 1440p
MonitorPowerCaseAudio
Dell U2311h (Portrait) Corsair HX520 NZXT Phantom White Asus Xonar D2X -> Samson SR850 or HiFi
 My System (16 items)
CPUMotherboardGraphicsRAM
i7 5820K @4.4ghz Asus X99-S EVGA GTX680 2GB 32GB Crucial 2133MHz DDR4
Hard DriveCoolingOSMonitor
120GB Samsung 830 | 120GB Samsung 840 EVO | 6TB... Noctua NH-D14 Windows 8.1 Achieva Shimian 27" 1440p
MonitorPowerCaseAudio
Dell U2311h (Portrait) Corsair HX520 NZXT Phantom White Asus Xonar D2X -> Samson SR850 or HiFi
+Rep to everyone

This is the code I have:
Code:
``````import math
circleheight = 25
```

When I analyze this code I get the error message:
Quote:
Traceback (most recent call last):
File "C:\Users\keith\untitled-1.py", line 5, in
NameError: name 'math' is not defined

Edited by BizzareRide - 1/29/12 at 4:41am
 Po' Pimpin' (11 items)
CPUMotherboardRAMHard Drive
i5 2500k @ stock Biostar TZ68K+ [A3] 4GB  Sandforce 1222 64GB SSD
Optical DriveCoolingOSMonitor
LG 22x DVD-+RW  Stock Windows 7 x64 Acer S211HL 1080p
PowerCaseMouse
 Po' Pimpin' (11 items)
CPUMotherboardRAMHard Drive
i5 2500k @ stock Biostar TZ68K+ [A3] 4GB  Sandforce 1222 64GB SSD
Optical DriveCoolingOSMonitor
LG 22x DVD-+RW  Stock Windows 7 x64 Acer S211HL 1080p
PowerCaseMouse
Quote:
Originally Posted by Scorpii

He just defined them. When you type:
something = whatever
You're setting the variable 'something' equal to the element 'whatever'.
See here for a quick basic introduction to python.
It's pretty good, and should get you understanding more in no time!
http://hetland.org/writing/instant-python.html

Thats a great website. I'm pretty much a neophyte as I just opened up Wing IDE a few days ago o.O
 Po' Pimpin' (11 items)
CPUMotherboardRAMHard Drive
i5 2500k @ stock Biostar TZ68K+ [A3] 4GB  Sandforce 1222 64GB SSD
Optical DriveCoolingOSMonitor
LG 22x DVD-+RW  Stock Windows 7 x64 Acer S211HL 1080p
PowerCaseMouse
 Po' Pimpin' (11 items)
CPUMotherboardRAMHard Drive
i5 2500k @ stock Biostar TZ68K+ [A3] 4GB  Sandforce 1222 64GB SSD
Optical DriveCoolingOSMonitor
LG 22x DVD-+RW  Stock Windows 7 x64 Acer S211HL 1080p
PowerCaseMouse
Nevermind I figured it out. Apparently, Python didn't like my indentation
 Po' Pimpin' (11 items)
CPUMotherboardRAMHard Drive
i5 2500k @ stock Biostar TZ68K+ [A3] 4GB  Sandforce 1222 64GB SSD
Optical DriveCoolingOSMonitor
LG 22x DVD-+RW  Stock Windows 7 x64 Acer S211HL 1080p
PowerCaseMouse
 Po' Pimpin' (11 items)
CPUMotherboardRAMHard Drive
i5 2500k @ stock Biostar TZ68K+ [A3] 4GB  Sandforce 1222 64GB SSD
Optical DriveCoolingOSMonitor
LG 22x DVD-+RW  Stock Windows 7 x64 Acer S211HL 1080p
PowerCaseMouse
I love so far is how you can define your own variable. Everyone that coded about me have slight different variable names, but our outcomes are the same. Thats really cool.
 Po' Pimpin' (11 items)
CPUMotherboardRAMHard Drive
i5 2500k @ stock Biostar TZ68K+ [A3] 4GB  Sandforce 1222 64GB SSD
Optical DriveCoolingOSMonitor
LG 22x DVD-+RW  Stock Windows 7 x64 Acer S211HL 1080p
PowerCaseMouse
 Po' Pimpin' (11 items)
CPUMotherboardRAMHard Drive
i5 2500k @ stock Biostar TZ68K+ [A3] 4GB  Sandforce 1222 64GB SSD
Optical DriveCoolingOSMonitor
LG 22x DVD-+RW  Stock Windows 7 x64 Acer S211HL 1080p
PowerCaseMouse
Quote:
Originally Posted by BizzareRide

I love so far is how you can define your own variable. Everyone that coded about me have slight different variable names, but our outcomes are the same. Thats really cool.

Guy's we need to watch out. We have just unleashed a beast into this world! But ya, python is nice and friendly.
 Skyship (13 items)
CPUMotherboardGraphicsRAM
Phenom II 1090T BE ASUS Crosshair IV XFX 5770 OCZ AMD Edition
Hard DriveOSMonitorKeyboard
Seagate Windows 7 Pro Sceptre 22" widescreen Saitek Eclipse 1