#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Posts: 3Member
[b][red]This message was edited by streak118 at 2006-5-1 0:1:14[/red][/b][hr]
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 " A) Air"
print " W) Water"
print " S) Steel"

#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 "
It 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.

• Posts: 2,900Member
: [b][red]This message was edited by streak118 at 2006-5-1 0:1:14[/red][/b][hr]
: 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.
:
: [code]
: 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 " A) Air"
: print " W) Water"
: print " S) Steel"
:
:
: #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 "
It will take", seconds, "second(s) for the sound wave to travel."
: [/code]

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:

[code]
def main():
blah blah blah...

main()
[/code]

: 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, [leftbr]code[rightbr] and [leftbr]/code[rightbr]

[size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

[code]
\$ select * from users where clue > 0
no rows returned
[/code]

• Posts: 2,900Member
: 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 "
It 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:

[code]
#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 " A) Air"
print " W) Water"
print " S) Steel"

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

#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 "
It will take", seconds, "second(s) for the sound wave to travel."

if __name__ == '__main__':
main()
[/code]

[size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

[code]
\$ select * from users where clue > 0
no rows returned
[/code]

• Posts: 3Member
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..

[code]
#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 " A) Air"
print " W) Water"
print " S) Steel"

#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