@lalarukh (Lalarukh khan) posted a new topic, subject "Can't show the input value result as distance". It read:

   Hi,  
    This is Lalarukh, I'm seeking community experts help.  
    Actually I want to show the distance between two entities that is getting as input from User. I'm taking them as simple code. Just want to get the two inputs which are working perfectly and then verify that input value from user is sun & mercury, after verification it should show the distance value as output.  
    There's no thing like to sum the distance just want to check the input value & assign the distance between entities.  
    Here's the code:
    [code] 
         def main() do
            IO.puts "Find the distance: "
            entity1 = IO.gets "Enter First Entity Name: "
            entity2 = IO.gets "Enter Second Entity Name: "
            if [entity1, entity2] = {"sun", "mercury"} do
              distance = "57909227";
            end
            IO.puts "Difference between #{String.trim(entity1)} & #{String.trim(entity2)} is: #{String.trim(distance)}km"
          end
        
    [/code]
    
    Sun & Mercury distance is one of example. I want to show the distance of more entities like EARTH & MOON or Sun & Neptune.
    
    Thanks

So @tomkonidas (Tom Konidas) replied

    lalarukh:
    
    > 
    [code]
    >      def main() do
    >         IO.puts "Find the distance: "
    >         entity1 = IO.gets "Enter First Entity Name: "
    >         entity2 = IO.gets "Enter Second Entity Name: "
    >         if [entity1, entity2] = {"sun", "mercury"} do
    >           distance = "57909227";
    >         end
    >         IO.puts "Difference between #{String.trim(entity1)} & #{String.trim(entity2)} is: #{String.trim(distance)}km"
    >       end
    >     
    [/code]
    
    You could do something like this:
    [code] 
          def main do
            IO.puts("Find the distance: ")
            entity1 = IO.gets("Enter First Entity Name: ") |> String.trim("\n")
            entity2 = IO.gets("Enter Second Entity Name: ") |> String.trim("\n")
        
            distance = distance(entity1, entity2)
        
            IO.puts("Difference between #{entity1} & #{entity2} is: #{distance}km")
          end
        
          defp distance("sun", "mercury"), do: "57909227"
          defp distance("mercury", "sun"), do: "57909227"
        
    [/code]
    
    You can keep on adding to the private `distance/2` function to add more support. and then later on generate logic for the sun → mercury, mercury → sun duplications

Then @dimitarvp (Dimitar Panayotov) said

    Which part is difficult for you? Seems you have it mostly figured out.
    
    Looking at your code, maybe you're struggling to do pattern matching, maybe?

So @lalarukh (Lalarukh khan) replied

    Hey @tomkonidas,  
    Thanks a lot. It worked the way I wanted It too   
    You helped me out, Thanks again.


{'source': 'discourse', 'url': 'https://elixirforum.com/t/cant-show-the-input-value-result-as-distance/46770', 'title': "Can't show the input value result as distance", 'views': 219, 'has_accepted_answer': False, 'word_count': 359, 'like_count': 3, 'reply_count': 1, 'participant_count': 3, 'posts': 4, 'tags': 'deployment learning-elixir troubleshooting', 'author': '@lalarukh (Lalarukh khan)', 'participants': '@lalarukh (Lalarukh khan), @tomkonidas (Tom Konidas), @dimitarvp (Dimitar Panayotov)', 'image_url': [None], 'created_at': '2022-03-23T14:10:39.927Z', 'last_posted_at': '2022-03-23T15:20:16.242Z'}