473,804 Members | 4,518 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

14 New Member
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.writ e( "Enter a value to convert: ")

# Read in a line from the keyboard
lineElements = sys.stdin.readl ine().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(lineElem ents[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(lineElem ents[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(lineElem ents[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(lineElem ents[1]) * 2.54))
Oct 31 '06 #1
6 3874
bartonc
6,596 Recognized Expert Expert
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
127 Recognized Expert New Member
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.writ e( "Enter a value to convert: ")

# Read in a line from the keyboard
lineElements = sys.stdin.readl ine().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(lineElem ents[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(lineElem ents[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(lineElem ents[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(lineElem ents[1]) * 2.54))
Nov 1 '06 #3
bratiskovci
14 New Member
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.writ e( "Enter a value to convert: ")

# Read in a line from the keyboard
lineElements = sys.stdin.readl ine().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(lineElem ents[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(lineElem ents[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(lineElem ents[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(lineElem ents[1]) * 2.54))
Nov 1 '06 #4
bartonc
6,596 Recognized Expert Expert
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
bratiskovci
14 New Member
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
6,596 Recognized Expert Expert
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

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

Similar topics

354
15932
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets hooked up to interfaces. The Gtk is more than enough gui.
14
2791
by: Chris Mantoulidis | last post by:
I am not clear with the use of the keyword inline... I believe you add it do a function when you implement the function inside the header file where the class is stored... But is that all? What am I missing? If that's all, then why did Bjarne even bother adding it to the language? If that's not all, what else can I do with "inline"?
47
3887
by: Richard Hayden | last post by:
Hi, I have the following code: /******************************** file1.c #include <iostream> extern void dummy(); inline int testfunc() {
18
5069
by: Method Man | last post by:
If I don't care about the size of my executable or compile time, is there any reason why I wouldn't want to inline every function in my code to make the program run more efficient?
10
408
by: Dave | last post by:
How do I write an inline function in c#? I've tried and looked at tutorials and other stuff but haven't been able to figure it out. if someone could give me a blank example, that would be very nice. thanks dave
19
17521
by: Riko Wichmann | last post by:
hi everyone, I'm googeling since some time, but can't find an answer - maybe because the answer is 'No!'. Can I call a function in python inline, so that the python byte compiler does actually call the function, but sort of inserts it where the inline call is made? Therefore avoiding the function all overhead. Thanks and cheers,
7
1976
by: jamihuq | last post by:
Hello, I would like to convert the following inline function to a macro. Can someone help? Thx Jami inline char * fromDESC(const char * &aDesC)
9
2904
by: chinu | last post by:
hi all, i did a small experiment to grasp the advantages of declaring a function as inline. inline int fun1(); int main(){ unsigned int start=0,end=0; asm("rdtsc\n\t"
25
2032
by: toton | last post by:
Hi, As inline is not mandetory, it depends on compiler to inline certain function (or using switch like fior GCC), my question is there any scope for inlining when it is not declared as inline function? i.e compiler may choose not to inline certain inline function, but is it free to choose a non inline function to inline it? I have some simple one line get function, and index operators which I want to get inlined. But due to some problem...
5
2579
by: V | last post by:
Hello: Consider the following recursive function: inline u64 multiplyPower(u64 V, u8 i, u64 c) { if (i == 0) return V; else return mul( multiplyPower(V,i-1,c) , c);
0
10577
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5521
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.