462,304 Members | 720 Online
Need help? Post your question and get tips & solutions from a community of 462,304 IT Pros & Developers. It's quick & easy.

converting??? [Inline code to function with loop]

 P: 14 1. How do I change the program so that the program does not terminate after completing one conversion. Instead, the program should continue to convert values until the user indicates that he/she wishes to quit. 2. How do you change the structure of the program so that ALL conversion computations are in a function of their own. Input: the user inputs the amount in one of the following forms: # C value # F value # CM value # I value # # C - is a celcius value. Program converts to fahrenheit # F - is a fahrenheit value. Program converts to celcius # CM - is a centimetre value. Program converts to inches # I - is an inch value. Program converts to centimetres import sys # Prompt user sys.stdout.write( "Enter a value to convert: ") # Read in a line from the keyboard lineElements = sys.stdin.readline().split(" ") # If the value is Celcius, convert to Fahrenheit if lineElements[0].lower() == 'c': print "%s degress celcius is %.2f degrees fahrenheit." \ % ( lineElements[1].strip(), (float(lineElements[1])*9.0/5.0)+32) # If the value is Fahrenheit, convert to Celcius elif lineElements[0].lower() == 'f': print "%s degress fahrenheit is %.2f degrees celcius." \ % ( lineElements[1].strip(), (float(lineElements[1]) - 32) * 5.0/9.0) # if the value is in centimetres, convert to inches elif lineElements[0].lower() == 'cm': print "%s centimetres is %.2f inches." % ( lineElements[1].strip(), (float(lineElements[1]) * 0.3937)) # if the value is in inches, convert to centimetres elif lineElements[0].lower() == 'i': print "%s inches is %.2f centimetres." % ( lineElements[1].strip(), (float(lineElements[1]) * 2.54)) Oct 31 '06 #1
6 Replies

 Expert 5K+ P: 6,596 Please post back using [ code ] [ /code ] (but without spaces) tags around your code. Otherwise, we can't see the structure of you code. Thanks. for example Expand|Select|Wrap|Line Numbers   # this is code   Oct 31 '06 #2

 Expert 100+ P: 127 To answer the first question, do the following: Expand|Select|Wrap|Line Numbers import sys   input = ""   while(input != "quit\n"):  input = sys.stdin.readline()  print "you typed : " + input   You see what happens here? We do repeat (through the while loop) until input is equal to "quit\n". I think you are able to figure out question 2 for yourself. -kudos 1. How do I change the program so that the program does not terminate after completing one conversion. Instead, the program should continue to convert values until the user indicates that he/she wishes to quit. 2. How do you change the structure of the program so that ALL conversion computations are in a function of their own. Input: the user inputs the amount in one of the following forms: # C value # F value # CM value # I value # # C - is a celcius value. Program converts to fahrenheit # F - is a fahrenheit value. Program converts to celcius # CM - is a centimetre value. Program converts to inches # I - is an inch value. Program converts to centimetres import sys # Prompt user sys.stdout.write( "Enter a value to convert: ") # Read in a line from the keyboard lineElements = sys.stdin.readline().split(" ") # If the value is Celcius, convert to Fahrenheit if lineElements[0].lower() == 'c': print "%s degress celcius is %.2f degrees fahrenheit." \ % ( lineElements[1].strip(), (float(lineElements[1])*9.0/5.0)+32) # If the value is Fahrenheit, convert to Celcius elif lineElements[0].lower() == 'f': print "%s degress fahrenheit is %.2f degrees celcius." \ % ( lineElements[1].strip(), (float(lineElements[1]) - 32) * 5.0/9.0) # if the value is in centimetres, convert to inches elif lineElements[0].lower() == 'cm': print "%s centimetres is %.2f inches." % ( lineElements[1].strip(), (float(lineElements[1]) * 0.3937)) # if the value is in inches, convert to centimetres elif lineElements[0].lower() == 'i': print "%s inches is %.2f centimetres." % ( lineElements[1].strip(), (float(lineElements[1]) * 2.54)) Nov 1 '06 #3

 P: 14 here is the code... Input: the user inputs the amount in one of the following forms: # C value # F value # CM value # I value # # C - is a celcius value. Program converts to fahrenheit # F - is a fahrenheit value. Program converts to celcius # CM - is a centimetre value. Program converts to inches # I - is an inch value. Program converts to centimetres import sys # Prompt user sys.stdout.write( "Enter a value to convert: ") # Read in a line from the keyboard lineElements = sys.stdin.readline().split(" ") # If the value is Celcius, convert to Fahrenheit if lineElements[0].lower() == 'c': print "%s degress celcius is %.2f degrees fahrenheit." \ % ( lineElements[1].strip(), (float(lineElements[1])*9.0/5.0)+32) # If the value is Fahrenheit, convert to Celcius elif lineElements[0].lower() == 'f': print "%s degress fahrenheit is %.2f degrees celcius." \ % ( lineElements[1].strip(), (float(lineElements[1]) - 32) * 5.0/9.0) # if the value is in centimetres, convert to inches elif lineElements[0].lower() == 'cm': print "%s centimetres is %.2f inches." % ( lineElements[1].strip(), (float(lineElements[1]) * 0.3937)) # if the value is in inches, convert to centimetres elif lineElements[0].lower() == 'i': print "%s inches is %.2f centimetres." % ( lineElements[1].strip(), (float(lineElements[1]) * 2.54)) Nov 1 '06 #4

 Expert 5K+ P: 6,596 Your post is still not quite right. Please read the sticky at the top of this forum or posting guidelines in that panel to the right or the gray lettering in the background when you are typing in your post. You can edit you post until it look like this: Expand|Select|Wrap|Line Numbers ### Prompt user ##sys.stdout.write( "Enter a value to convert: ") ## ### Read in a line from the keyboard ##lineElements = sys.stdin.readline().split(" ") # define the function def convert(selector, valStr, value):     # if the value is in centimetres, convert to inches     elif selector == 'cm': # test for this first         print "%s centimetres is %.2f inches." % (valStr,         value * 0.3937)     # If the value is Celcius, convert to Fahrenheit     if selector.startswith('c'): # could be 'ce'         print "%s degress celcius is %.2f degrees fahrenheit." \         % (valStr, (value * 9.0/5.0) + 32)     # If the value is Fahrenheit, convert to Celcius     elif selector.startswith('f'):         print "%s degress fahrenheit is %.2f degrees celcius." \         % (valStr, (value - 32) * 5.0/9.0)    # if the value is in inches, convert to centimetres     elif selector.startswith('i'): # allows userInput 'in'         print "%s inches is %.2f centimetres." % (valStr,         value * 2.54) # Initialize some variables convertTypes = ('ce', 'fe', 'cm', 'in', 'c', 'f', 'i') # Use a while loop, waiting for and empty string or 'q' while True:     # Prompt and read at one time     userInput = raw_input("Enter a value to convert or q to quit: ")     userInput = userInput.lower() # call lower() method on the whole string     if len(userInput) < 1 or userInput.startswith('q'):         break # break out of loop. use len() in case you use userInput[0] instead     lineElements = userInput.split()    # split ANY whitespace - use lots of variables for debugging     selChar, value = lineElements[0][0:2], float(lineElements[1])     # First, make sure that you have good data to pass     if selChar in convertTypes:         # Then call the funtion with the data         # This separates input checking from converting [VERY GOOD]         convert(selChar, lineElements[1].strip(), value)     else:         print "can not convert that..."   You can also preview your post before you hit "Submit". Thanks, Barton Nov 1 '06 #5

 P: 14 Expand|Select|Wrap|Line Numbers import sys   # dictionary of day names for output. dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday",  5:"Thursday", 6:"Friday"}   # Get user input:  month, day, year sys.stdout.write("Please enter the day of the month (1-31): ") dayOfMonth = int(sys.stdin.readline().strip())   sys.stdout.write("Please enter the month (1-12): ") month = int(sys.stdin.readline().strip())   sys.stdout.write("Please enter the year: ") year = int(sys.stdin.readline().strip())   # Make adjustments for forumla  if month == 1 or month == 2:     month = month + 12     year = year - 1   # Compute coefficients century = year/100 yearInCentury = year % 100   # Compute the day using Zeller's congruence dayOfWeek = (dayOfMonth + ((month + 1)* 26)/10 + yearInCentury +              (yearInCentury/4) + (century/4) - 2*century) % 7   # Output answer print "Day of week is %s" % dayNames[dayOfWeek] Nov 3 '06 #6

 Expert 5K+ P: 6,596 Expand|Select|Wrap|Line Numbers import sys   # dictionary of day names for output. dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday", 5:"Thursday", 6:"Friday"}   # Get user input: month, day, year sys.stdout.write("Please enter the day of the month (1-31): ") dayOfMonth = int(sys.stdin.readline().strip())   sys.stdout.write("Please enter the month (1-12): ") month = int(sys.stdin.readline().strip())   sys.stdout.write("Please enter the year: ") year = int(sys.stdin.readline().strip())   # Make adjustments for forumla  if month == 1 or month == 2: month = month + 12 year = year - 1   # Compute coefficients century = year/100 yearInCentury = year % 100   # Compute the day using Zeller's congruence dayOfWeek = (dayOfMonth + ((month + 1)* 26)/10 + yearInCentury + (yearInCentury/4) + (century/4) - 2*century) % 7   # Output answer print "Day of week is %s" % dayNames[dayOfWeek] Awesome! You can post using tags! That's great. Now you have some more things to work on after you copy the pieces from my last posts. You CAN copy and paste to and from the forum, right? Nov 3 '06 #7