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))
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 kudos 127
Recognized Expert New Member
To answer the first question, do the following: -
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.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))
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))
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: -
### 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
- 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]
bartonc 6,596
Recognized Expert Expert - 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?
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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"?
|
by: Richard Hayden |
last post by:
Hi,
I have the following code:
/******************************** file1.c
#include <iostream>
extern void dummy();
inline int testfunc() {
|
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?
|
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
| |
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,
|
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)
|
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"
|
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...
|
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);
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |