Results 1 to 5 of 5

Thread: Programming in python

  1. #1
    Join Date
    Jul 2009
    Posts
    122

    Programming in python

    I need help with programming in python. The following statement:
    a, b, c refer to three lengths. Determine if it can construct a triangle. If yes, specify the type of triangle.

    That's what I did, it seems a little more complicated and it does not work, though I find the problem I can not find it!

    Code:
    from math import * 
     
    a=raw_input("dimension of the side BC: " ) 
    b=raw_input("dimension of the side AC: " ) 
    c=raw_input("dimension of the side AB: " ) 
    a=float(a) 
    b=float(b) 
    c=float(c) 
     
    x1=asin(a/c) 
    x2=asin(b/c) 
    x3=atan(c/b) 
     
    x=x1+x2+x3 
     
    if x==pi: 
        if x1==(pi/2) or x2==(pi/2) or x3==(pi/2): 
            print "ABC is a triangle" 
        elif x1==x2 or x2==x3 or x1==x3: 
            print "ABC is a isosceles triangle" 
        elif x1==x2 and x2==x3 and x3==(pi/3): 
            print "ABC is an equilateral triangle" 
        else: 
            print "ABC is any triangle" 
    else: 
        print "ABC isn't a triangle"

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: Programming in python

    Hello, a few remarks about your program: it works well but ...
    (1) add the line

    Code:
    #  -*- coding: utf-8 -*-
    at the beginning of your code, if you have a problem with accents in your text
    (2) does not
    Code:
    from math import *
    it seems simple but in reality it is a source of error, for later, when your code has grown.

    (3) instead of:
    Code:
    a = float (a)
    b = float (b)
    c = float (c)
    You can also write:

    Code:
    a, b, c = float (a), float (b), float (c)

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Programming in python

    Quote Originally Posted by Katty View Post
    Hello, a few remarks about your program: it works well but ...
    Not necessarily, it depends potential platform: it makes strict equality on floating

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Programming in python

    You must use an epsilon (basically you determine a margin of error, you subtracted the two numbers to compare and if the result is less than your margin of error you consider that they are equal).

    There are other strategies, but they impose limitations (and potentially impact the level of performance) and are not necessarily used as appropriate: to make his calculations with integers (by adding powers of 10 baseline, this is how the calculations are made monetary) or by the fixed-point arithmetic or decimal. In python, the role of the decimal module precisely.

  5. #5
    Join Date
    Mar 2011
    Posts
    1

    Re: Programming in python

    am not sure why u wrote this code like that but but i made a few changes to it myself
    Code:
    #  -*- coding: utf-8 -*-
    import math 
     
    a=raw_input("dimension of the side BC: " ) 
    b=raw_input("dimension of the side AC: " ) 
    c=raw_input("dimension of the side AB: " ) 
    a=float(a) 
    b=float(b) 
    c=float(c) 
     
    x1 = math.sin(a/c) 
    x2 = math.sin(b/c) 
    x3 = math.tan(c/b) 
     
    x=x1+x2+x3
    pi=3.142
     
    if x==pi: 
        if x1==(pi/2) or x2==(pi/2) or x3==(pi/2): 
            print "ABC is a triangle" 
        elif x1==x2 or x2==x3 or x1==x3: 
            print "ABC is a isosceles triangle" 
        elif x1==x2 and x2==x3 and x3==(pi/3): 
            print "ABC is an equilateral triangle" 
        else: 
            print "ABC is any triangle" 
    else: 
        print "ABC isn't a triangle"

Similar Threads

  1. Good books for learning Python programming language
    By afroditaa in forum Software Development
    Replies: 6
    Last Post: 21-01-2013, 10:42 AM
  2. Replies: 6
    Last Post: 20-12-2012, 06:37 PM
  3. Download Python 3.0 / Python 3000
    By Amaresh in forum Software Development
    Replies: 6
    Last Post: 24-02-2009, 09:28 AM
  4. Replies: 3
    Last Post: 13-12-2008, 01:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,422,459.72437 seconds with 16 queries