By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,304 Members | 720 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
6 Replies

bartonc
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
  1.  
  2. # this is code
  3.  
Oct 31 '06 #2

kudos
Expert 100+
P: 127
To answer the first question, do the following:

Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. input = ""
  4.  
  5. while(input != "quit\n"):
  6.  input = sys.stdin.readline()
  7.  print "you typed : " + input
  8.  
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

bartonc
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
  1. ### Prompt user
  2. ##sys.stdout.write( "Enter a value to convert: ")
  3. ##
  4. ### Read in a line from the keyboard
  5. ##lineElements = sys.stdin.readline().split(" ")
  6. # define the function
  7. def convert(selector, valStr, value):
  8.     # if the value is in centimetres, convert to inches
  9.     elif selector == 'cm': # test for this first
  10.         print "%s centimetres is %.2f inches." % (valStr,
  11.         value * 0.3937)
  12.     # If the value is Celcius, convert to Fahrenheit
  13.     if selector.startswith('c'): # could be 'ce'
  14.         print "%s degress celcius is %.2f degrees fahrenheit." \
  15.         % (valStr, (value * 9.0/5.0) + 32)
  16.     # If the value is Fahrenheit, convert to Celcius
  17.     elif selector.startswith('f'):
  18.         print "%s degress fahrenheit is %.2f degrees celcius." \
  19.         % (valStr, (value - 32) * 5.0/9.0)    # if the value is in inches, convert to centimetres
  20.     elif selector.startswith('i'): # allows userInput 'in'
  21.         print "%s inches is %.2f centimetres." % (valStr,
  22.         value * 2.54)
  23. # Initialize some variables
  24. convertTypes = ('ce', 'fe', 'cm', 'in', 'c', 'f', 'i')
  25. # Use a while loop, waiting for and empty string or 'q'
  26. while True:
  27.     # Prompt and read at one time
  28.     userInput = raw_input("Enter a value to convert or q to quit: ")
  29.     userInput = userInput.lower() # call lower() method on the whole string
  30.     if len(userInput) < 1 or userInput.startswith('q'):
  31.         break # break out of loop. use len() in case you use userInput[0] instead
  32.     lineElements = userInput.split()    # split ANY whitespace - use lots of variables for debugging
  33.     selChar, value = lineElements[0][0:2], float(lineElements[1])
  34.     # First, make sure that you have good data to pass
  35.     if selChar in convertTypes:
  36.         # Then call the funtion with the data
  37.         # This separates input checking from converting [VERY GOOD]
  38.         convert(selChar, lineElements[1].strip(), value)
  39.     else:
  40.         print "can not convert that..."
  41.  
You can also preview your post before you hit "Submit". Thanks,
Barton
Nov 1 '06 #5

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

bartonc
Expert 5K+
P: 6,596
Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. # dictionary of day names for output.
  4. dayNames = {0:"Saturday", 1:"Sunday", 2:"Monday", 3:"Tuesday", 4:"Wednesday",
  5. 5:"Thursday", 6:"Friday"}
  6.  
  7. # Get user input: month, day, year
  8. sys.stdout.write("Please enter the day of the month (1-31): ")
  9. dayOfMonth = int(sys.stdin.readline().strip())
  10.  
  11. sys.stdout.write("Please enter the month (1-12): ")
  12. month = int(sys.stdin.readline().strip())
  13.  
  14. sys.stdout.write("Please enter the year: ")
  15. year = int(sys.stdin.readline().strip())
  16.  
  17. # Make adjustments for forumla 
  18. if month == 1 or month == 2:
  19. month = month + 12
  20. year = year - 1
  21.  
  22. # Compute coefficients
  23. century = year/100
  24. yearInCentury = year % 100
  25.  
  26. # Compute the day using Zeller's congruence
  27. dayOfWeek = (dayOfMonth + ((month + 1)* 26)/10 + yearInCentury +
  28. (yearInCentury/4) + (century/4) - 2*century) % 7
  29.  
  30. # Output answer
  31. 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

Post your reply

Sign in to post your reply or Sign up for a free account.