443,965 Members | 1,962 Online
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
5 Replies

 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

 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 convert_types = ("C", "F")    # tuple for checking user input against def GetConversionType():     while True:    # Loop until we get good data or an empty string         inputStr = raw_input("Enter \"F\"ahrenheit or \"C\"elcius: ")         inputStr = inputStr.upper()    # let user type lower case         if inputStr in convert_types or inputStr == "":             break    # break out of the loop     return inputStr    # return a value to the caller   def GetIntegerValue():     # integers only for now, floating point is a little harder     while True:    # Loop until we get good data or an empty string         inputStr = raw_input("Enter a value to convert: ")         if inputStr.isdigit():             return int(inputStr)    # return will also break out of the loop         elif inputStr == "":             return None    # None is a good way to signal "no data"   def CtoF(deg_c):     pass    # This part is for you to figure out def FtoC(deg_f):     return deg_f    # Use of a passed in parameter   convert_type = GetConversionType() or convert_types[0] # shortcut to defalt type value = GetIntegerValue()   if value is not None:    # only work on valid data     if convert_type == convert_types[0]:         print CtoF(value)     elif convert_type == convert_types[1]:         print FtoC(value) else:     print "No conversion due to bad data"   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 convert_types = ("C", "F")    # tuple for checking user input against def GetConversionType():     while True:    # Loop until we get good data or an empty string         inputStr = raw_input("Enter \"F\"ahrenheit or \"C\"elcius: ")         inputStr = inputStr.upper()    # let user type lower case         if inputStr in convert_types or inputStr == "":             break    # break out of the loop     return inputStr    # return a value to the caller   def GetIntegerValue():     # integers only for now, floating point is a little harder     while True:    # Loop until we get good data or an empty string         inputStr = raw_input("Enter a value to convert: ")         if inputStr.isdigit():             return int(inputStr)    # return will also break out of the loop         elif inputStr == "":             return None    # None is a good way to signal "no data"   def CtoF(deg_c):     pass    # This part is for you to figure out def FtoC(deg_f):     return deg_f    # Use of a passed in parameter   convert_type = GetConversionType() or convert_types[0] # shortcut to defalt type value = GetIntegerValue()   if value is not None:    # only work on valid data     if convert_type == convert_types[0]:         print CtoF(value)     elif convert_type == convert_types[1]:         print FtoC(value) else:     print "No conversion due to bad data"   Oct 20 '06 #5

 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 planet_dist_dict = {"Earth":80000, "Mars":500000}   # check input and get values with a dictionary # make a loop like before so valid planet names get entered # use inputStr.capitalize() so user's case doesn't matter inputStr = raw_input("Enter Planet Name: ") if inputStr in planet_dist_dict:    # if it's in the dict, use it a subscript     print planet_dist_dict[inputStr]   print planet_dist_dict.get("foo") # dict.get() handles non-existant items   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.