# ------------------------------------------------------------ #Student: Austin Wells #Class: CIS122 #Date: 4/13/2015 #Program Title: Chapter 4 Programming Excercise 3.py #(Mass and Weight) #Program Description: The program prompts the user for an objects #mass. If the object weighs more than 1000 Newtons it will display #a message that the object was to heavy, If the object is less than 10 #Newtons, a message will display its too light. # ------------------------------------------------------------ GRAVITY_CONST = 9.8 # evaluates the weight and prints out if it is too heavy or light def measureWeight(weight): if (weight > 1000.0): print 'The weight of {} Newtons is too heavy' .format(weight) elif (weight < 10.0): print 'The weight of {} Newtons is too light' .format(weight) else: print 'The weight of {} Newtons is just right' .format(weight) # prompts user for input. def promptForNumber(strPrompt): userInput = float(raw_input(strPrompt + ": ")) print '{}' .format(userInput) return userInput #calculates weight def calculateWeight(userInputMass): weight = userInputMass * GRAVITY_CONST return weight #main driver function def main(): try: userInput = promptForNumber("Please enter an objects mass in kilograms") except (ValueError): print '\n\nInvalid input. You have entered a non integer. Please enter an objects mass' except (EOFError): print '\n\nInvalid input. You have forgotten to add input. Please Reset program and Please enter the objects mass' else: weightCalculated = calculateWeight(userInput) measureWeight(weightCalculated) main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question