By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

Need Help with code strucure [solved]

P: 14
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
Share this Question
Share on Google+
5 Replies


bartonc
Expert 5K+
P: 6,596
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

P: 14
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
Expert 5K+
P: 6,596
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

P: 14
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
Expert 5K+
P: 6,596
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 discussion thread is closed

Replies have been disabled for this discussion.