473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need Help with code strucure [solved]

Hello,

I am trying to write a few programs using python but don't really know where to start...I am completely CONFUSED.

My first program deals with conversion between fahrenheit and celcius and between centimetres and inches.
example:
Enter a value to convert: C100
100 degrees celcius is 212.0 degrees fahrenheit.

My second problem deal with Zellers Congruence, which a program that tells us upon which day of the week a given date falls.
Example:
Please enter the day of the month(1-31):1
Please enter the month(1-12):1
Please enter the year:2000
Day of week is Saturday

My third problem is trying to compute the distance between the sun and a planet in the solar system and or will compute the minimum and maximum distances betwenn 2 planets within our solar system.
Example:
Enter first planet:Sun
Enter second planet:Earth
Distance between Sun and Earth is 149600000 km

Example if 2 planets are specified:
Enter first planet:mercury
Enter second planet: Uranus
Minimum distance between mercury and Uranus is 2813100000 km
Maximum distance between Mercury and Uranus is 292890000 km

Thanks in advance.
Oct 19 '06 #1
5 1588
bartonc
6,596 Expert 4TB
OK, how confused are you? Can you create a file_name.py with IDLE and run it? Are you looking for help with code structure? Maybe you're working with the console or are you trying to build a GUI? I python install? What version is it and what computer are you using? We need details in order to be able you help you.

Thanks,
Barton
Oct 20 '06 #2
I need help with code structure...

OK, how confused are you? Can you create a file_name.py with IDLE and run it? Are you looking for help with code structure? Maybe you're working with the console or are you trying to build a GUI? I python install? What version is it and what computer are you using? We need details in order to be able you help you.

Thanks,
Barton
Oct 20 '06 #3
bartonc
6,596 Expert 4TB
Enter a value to convert: C100
100 degrees celcius is 212.0 degrees fahrenheit.
Let's simplify the input "parsing" by breaking it up into two parts:
Enter "F"ahrenheit or "C"elcius: C
Enter a value to convert: 100

Which means we want two functions which make sure that we get valid data:

Expand|Select|Wrap|Line Numbers
  1. convert_types = ("C", "F")    # tuple for checking user input against
  2. def GetConversionType():
  3.     while True:    # Loop until we get good data or an empty string
  4.         inputStr = raw_input("Enter \"F\"ahrenheit or \"C\"elcius: ")
  5.         inputStr = inputStr.upper()    # let user type lower case
  6.         if inputStr in convert_types or inputStr == "":
  7.             break    # break out of the loop
  8.     return inputStr    # return a value to the caller
  9.  
  10. def GetIntegerValue():
  11.     # integers only for now, floating point is a little harder
  12.     while True:    # Loop until we get good data or an empty string
  13.         inputStr = raw_input("Enter a value to convert: ")
  14.         if inputStr.isdigit():
  15.             return int(inputStr)    # return will also break out of the loop
  16.         elif inputStr == "":
  17.             return None    # None is a good way to signal "no data"
  18.  
  19. def CtoF(deg_c):
  20.     pass    # This part is for you to figure out
  21. def FtoC(deg_f):
  22.     return deg_f    # Use of a passed in parameter
  23.  
  24. convert_type = GetConversionType() or convert_types[0] # shortcut to defalt type
  25. value = GetIntegerValue()
  26.  
  27. if value is not None:    # only work on valid data
  28.     if convert_type == convert_types[0]:
  29.         print CtoF(value)
  30.     elif convert_type == convert_types[1]:
  31.         print FtoC(value)
  32. else:
  33.     print "No conversion due to bad data"
  34.  
Oct 20 '06 #4
this is helping alot and is much appreciated...
As I look further onto the other problems I believe that I can use this is a quide to them...they seem to have similar coding.
If you have any pointers or suggestions on the other problems your advice would be much appreciated.

Thanks again....
Let's simplify the input "parsing" by breaking it up into two parts:
Enter "F"ahrenheit or "C"elcius: C
Enter a value to convert: 100

Which means we want two functions which make sure that we get valid data:

Expand|Select|Wrap|Line Numbers
  1. convert_types = ("C", "F")    # tuple for checking user input against
  2. def GetConversionType():
  3.     while True:    # Loop until we get good data or an empty string
  4.         inputStr = raw_input("Enter \"F\"ahrenheit or \"C\"elcius: ")
  5.         inputStr = inputStr.upper()    # let user type lower case
  6.         if inputStr in convert_types or inputStr == "":
  7.             break    # break out of the loop
  8.     return inputStr    # return a value to the caller
  9.  
  10. def GetIntegerValue():
  11.     # integers only for now, floating point is a little harder
  12.     while True:    # Loop until we get good data or an empty string
  13.         inputStr = raw_input("Enter a value to convert: ")
  14.         if inputStr.isdigit():
  15.             return int(inputStr)    # return will also break out of the loop
  16.         elif inputStr == "":
  17.             return None    # None is a good way to signal "no data"
  18.  
  19. def CtoF(deg_c):
  20.     pass    # This part is for you to figure out
  21. def FtoC(deg_f):
  22.     return deg_f    # Use of a passed in parameter
  23.  
  24. convert_type = GetConversionType() or convert_types[0] # shortcut to defalt type
  25. value = GetIntegerValue()
  26.  
  27. if value is not None:    # only work on valid data
  28.     if convert_type == convert_types[0]:
  29.         print CtoF(value)
  30.     elif convert_type == convert_types[1]:
  31.         print FtoC(value)
  32. else:
  33.     print "No conversion due to bad data"
  34.  
Oct 20 '06 #5
bartonc
6,596 Expert 4TB
3) The very cool python structure that you need to use to solve a "lookup" of text mapped to a value is the dictionary!
Expand|Select|Wrap|Line Numbers
  1. planet_dist_dict = {"Earth":80000, "Mars":500000}   # check input and get values with a dictionary
  2. # make a loop like before so valid planet names get entered
  3. # use inputStr.capitalize() so user's case doesn't matter
  4. inputStr = raw_input("Enter Planet Name: ")
  5. if inputStr in planet_dist_dict:    # if it's in the dict, use it a subscript
  6.     print planet_dist_dict[inputStr]
  7.  
  8. print planet_dist_dict.get("foo") # dict.get() handles non-existant items
  9.  
Once you have your two distance looked up in the dictionary, say x and y, you might want to use abs() to get the absolute value (get rid of the minus sign in case someone enters, say, Mars Earth). Use zero a the sun's distance. Then start to learn about format strings (very handy for readable code and nice output):

dist = abs(x-y)
print "The distance between %s and %s is %d kilometers" %(planet1, planet2, dist)
Oct 21 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gregor Horvath | last post by:
Hi, Before I reinvent the wheel I`d like to ask if someone has done this before since I did not find an advice at Google. The goal is to create a dynamic Tree View in HTML. Say I have a...
7
by: Graham Taylor | last post by:
I've tried posting this in the 'microsoft.public.access' but I will post it here also, as I think it might be the webserver which is causing my problem. --------- I have an Access 2003 database...
48
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and...
6
by: erdem | last post by:
hi all, i guess this question had been asked many times but i couldnt find a way how can we generate a struct with managed code with fixed size i mean struct easy {
0
by: Peter Conrey | last post by:
I have a perl web service (using SOAP::Lite) with a method called "Detail" that returns a strucure (hash reference to be exact). It works fine when consumed by a Perl client, but when I try to...
3
by: Ralph Heger | last post by:
Hi An API-Function I want to use needs a structure as parameter: public structure myStruct dim i1 as short dim i2 as byte dim i3 as integer dim buffer as byte() end structure
18
by: vashwath | last post by:
Hi all, In my current project I am thinking to use setjmp/lngjmp for exception handling.The way I am planing to do this is shown in the below example. Please if this is the right way to do.Are...
2
by: noridotjabi | last post by:
I have a program (its gigantic so I'm not posting the source) that I'm writing with two probelms. First of all can someone give me an example of how fgets is suposed to be used because I've read...
3
by: babu198649 | last post by:
hi i have a structure xml_data struct xml_data { int prf; int *pVsipl_buffer; };
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.