Need Help with code strucure [solved]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bratiskovci
    New Member
    • Oct 2006
    • 14

    #1

    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.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    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

    Comment

    • bratiskovci
      New Member
      • Oct 2006
      • 14

      #3
      I need help with code structure...

      Originally posted by bartonc
      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

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by bratiskovci
        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"ahrenhei t or "C"elcius: C
        Enter a value to convert: 100

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

        Code:
        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"

        Comment

        • bratiskovci
          New Member
          • Oct 2006
          • 14

          #5
          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....
          Originally posted by bartonc
          Let's simplify the input "parsing" by breaking it up into two parts:
          Enter "F"ahrenhei t or "C"elcius: C
          Enter a value to convert: 100

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

          Code:
          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"

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            3) The very cool python structure that you need to use to solve a "lookup" of text mapped to a value is the dictionary!
            Code:
            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)

            Comment

            Working...