## Python

Moderators: None (Apply to moderate this forum)
Number of posts: 1166

This Forum Only

This message was edited by streak118 at 2006-5-1 0:1:14

ok... Im a tard... somebody, ANYBODY!, please help! tell me what I'm doing wrong? or better yet tell me what to fix and how to? this is a class project. I'm failing this class because Im not grasping programming... and I started with C++ then came to this, HELP!?!

#This program will calculate the time it will take a sound to travel based on
#the matter it is traveling through; air, water, or steel.

def main():

#declare and initialize variables
#AIR as constant
AIR = 1100

#WATER as constant
WATER = 4900

#STEEL as constant
STEEL = 16400

#feet as int
#feet = 0

#seconds as int
seconds = 0

#Intro
print "WELCOME TO THE SPEED OF SOUND PROGRAM!!!"
print "I will calculate the time it takes for a souond to travel through"
print "different matter."

#build menu for user to choose from

print "\tA)\tAir"
print "\tW)\tWater"
print "\tS)\tSteel"

#prompt user for distance of travel
feet = input("Now, please enter the distance that the sound wave will travel(feet): ")

#calculate time for sound to travel
A = AIR
W = WATER
S = STEEL

seconds = feet / AIR
seconds = feet / WATER
else:
seconds = feet / STEEL

#display ETA of the sound

print "\nIt will take", seconds, "second(s) for the sound wave to travel."

ps... I have this indented but it's not showing up on the message board.

: This message was edited by streak118 at 2006-5-1 0:1:14

: ok... Im a tard... somebody, ANYBODY!, please help! tell me what I'm doing wrong? or better yet tell me what to fix and how to? this is a class project. I'm failing this class because Im not grasping programming... and I started with C++ then came to this, HELP!?!
:
: #This program will calculate the time it will take a sound to travel based on
: #the matter it is traveling through; air, water, or steel.
:
:
```: def main():
:
: #declare and initialize variables
: #AIR as constant
: AIR = 1100
:
: #WATER as constant
: WATER = 4900
:
: #STEEL as constant
: STEEL = 16400
:
:
: #feet as int
: #feet = 0
:
: #seconds as int
: seconds = 0
:
: #Intro
: print "WELCOME TO THE SPEED OF SOUND PROGRAM!!!"
: print "I will calculate the time it takes for a souond to travel through"
: print "different matter."
:
: #build menu for user to choose from
:
: print "\tA)\tAir"
: print "\tW)\tWater"
: print "\tS)\tSteel"
:
:
: #prompt user for distance of travel
: feet = input("Now, please enter the distance that the sound wave will travel(feet): ")
:
: #calculate time for sound to travel
: A = AIR
: W = WATER
: S = STEEL
:
: seconds = feet / AIR
: seconds = feet / WATER
: else:
: seconds = feet / STEEL
:
: #display ETA of the sound
:
: print "\nIt will take", seconds, "second(s) for the sound wave to travel."
: ```

I'll take a closer look a little later, but for now, one quick lesson in Python. The interpreter does not automatically call any "main" function. All you've done here is define a function named "main" and then everything ends. You need to put a call to main after you define it:

```def main():
blah blah blah...

main()
```

: ps... I have this indented but it's not showing up on the message board.

You need to use the special formatting tags for this board system, [code] and [/code]

infidel

```\$ select * from users where clue > 0
no rows returned
```

: def main():
:
: #declare and initialize variables

technically you never "declare" variables in python, you just bind names to values.

: #AIR as constant
: AIR = 1100
:
: #WATER as constant
: WATER = 4900
:
: #STEEL as constant
: STEEL = 16400

I'm sure you know by now, but these aren't true "constants". Python has no such construct.

: #prompt user for distance of travel
: feet = input("Now, please enter the distance that the sound wave will travel(feet): ")

input() is risky because it tries to eval() whatever the user enters so can very easily trigger exceptions. Better to use raw_input and try to convert the value to a decimal.

: #calculate time for sound to travel
: A = AIR
: W = WATER
: S = STEEL

All you've done here is make two names both equal the same value A and AIR are now both references to the same integer object.

: seconds = feet / AIR
: seconds = feet / WATER
: else:
: seconds = feet / STEEL

The user entered strings, but here you're checking their input against the value of A. A is different than 'A' in Python.

: #display ETA of the sound
:
: print "\nIt will take", seconds, "second(s) for the sound wave to travel."

Like I mentioned in the other reply, you need to call your main function after defining it.

Ok, so here is what I have come up with:

```#This program will calculate the time it will take a sound to travel based on
#the matter it is traveling through; air, water, or steel.

# sound propagation "constants"
AIR = 1100
WATER = 4900
STEEL = 16400

def main():

#Intro
print "WELCOME TO THE SPEED OF SOUND PROGRAM!!!"
print "I will calculate the time it takes for a souond to travel through"
print "different matter."

#build menu for user to choose from
print "\tA)\tAir"
print "\tW)\tWater"
print "\tS)\tSteel"

#prompt user for choice
choice = ' '
while choice not in 'AaWwSs':

#prompt user for distance of travel
feet = float(
raw_input("Please enter the distance (feet) that the sound wave will travel: ")
)

#calculate time for sound to travel
seconds = 0
if choice in 'Aa':
seconds = feet / AIR
elif choice in 'Ww':
seconds = feet / WATER
elif choice in 'Ss':
seconds = feet / STEEL

#display ETA of the sound
print "\nIt will take", seconds, "second(s) for the sound wave to travel."

if __name__ == '__main__':
main()
```

infidel

```\$ select * from users where clue > 0
no rows returned
```

thank you so much for your help! unfortunantly the teacher never taught us the __whatever__ codes so if I used them it would look suspicious... lol... here's what I ended up with minus input validations..

```#This program will calculate the time it will take a sound to travel based on
#the matter it is traveling through; air, water, or steel.

def main():

#declare and initialize variables
#AIR as constant
AIR = 1100

#WATER as constant
WATER = 4900

#STEEL as constant
STEEL = 16400

#feet as int
#feet = 0

#seconds as int
seconds = 0

#Intro
print "WELCOME TO THE SPEED OF SOUND PROGRAM!!!"
print "I will calculate the time it takes for a souond to travel through"
print "different matter."

#build menu for user to choose from

print "\tA)\tAir"
print "\tW)\tWater"
print "\tS)\tSteel"

#prompt user for distance of travel
feet = input("Now, please enter the distance that the sound wave will travel(feet): ")

feet = float(feet)

#calculate time for sound to travel

seconds = feet / AIR
seconds =  feet / WATER
else:
seconds = feet / STEEL

#display ETA of the sound

print "\nIt will take", seconds, "second(s) for the sound wave to travel."
```

again thank you so much for helping, I'll need more help between now (may 1st, 10:10pm est) and tomorrow 5pm with three other programs I have to build... I hope you can help some more.

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic