5 Replies - 744 Views - Last Post: 18 December 2014 - 02:59 PM Rate Topic: -----

#1 FlapperJacker   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-November 14

What's wrong with this code?

Posted 18 December 2014 - 01:50 PM

When inputting equilateral triangle it works. While loop also works. I was able to get it working in c, but not python.

Python code:
while True:
    Side1 = input("Please enter side 1\n")
    Side2 = input("Please enter side 2 \n")
    Side3 = input("Please enter side 3 \n")
    if Side1==Side2 and Side2==Side3:
        print("The Triangle is Equilateral");
    elif Side1*Side1+Side2*Side2==Side3*Side3 or Side3*Side3+Side2*Side2==Side1*Side1 or Side1*Side1+Side3*Side3==Side2*Side2:
        print("It is a right triangle")
    else:
        printf("It is neither a right triangle or an equilateral triangle")
    y = input("\nWould you like to try again? 1 for yes, 2 for no.\n")
    if y==1:
        break






c code:

#include <stdio.h>

void inputFunction(float *, float *, float *);
void outputFunction(float, float, float);

int main()
{
float Side1,Side2,Side3;
int y=1;
while (y==1)
{
    inputFunction(&Side1,&Side2,&Side3);
    outputFunction(Side1,Side2,Side3);
    printf("\nWould you like to try again? 1 for yes, 2 for no.\n");
    scanf("%d", &y);

}
}
void inputFunction(float*Side1, float*Side2, float*Side3)
{
    printf("Please enter side 1\n");
    scanf("%f", Side1);
    printf("Please enter side 2 \n");
    scanf("%f", Side2);
    printf("Please enter side 3 \n");
    scanf("%f", Side3);
}

void outputFunction(float Side1, float Side2, float Side3)
{
    if (Side1==Side2 && Side2==Side3)
    {
        printf("The Triangle is Equilateral");
    }
    else if ((Side1*Side1)+(Side2*Side2)==(Side3*Side3) || (Side3*Side3)+(Side2*Side2)==(Side1*Side1) || (Side1*Side1)+(Side3*Side3)==(Side2*Side2))
    {
        printf("It is a right triangle");
    }
    else
    {
        printf("It is neither a right triangle or an equilateral triangle");
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: What's wrong with this code?

#2 andrewsw   User is online

  • Unprocessable Entity
  • member icon

Reputation: 6594
  • View blog
  • Posts: 26,833
  • Joined: 12-December 12

Re: What's wrong with this code?

Posted 18 December 2014 - 02:01 PM

If the C version works then why don't you use the same bracketing that you use in the C version in the Python version? I haven't inspected to see if it makes a difference, but it is what I would attempt first.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14352
  • View blog
  • Posts: 57,556
  • Joined: 12-June 08

Re: What's wrong with this code?

Posted 18 December 2014 - 02:01 PM

Quote

What's wrong with this code?

I was able to get it working in c, but not python.

Please elaborate. What is not working? *HOW* is it not working? etc.
Was This Post Helpful? 0
  • +
  • -

#4 FlapperJacker   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-November 14

Re: What's wrong with this code?

Posted 18 December 2014 - 02:14 PM

When I enter an equilateral triangle it works and when it asks if I want to try again it works. However when I enter a different type of triangle it doesn't work.
Traceback (most recent call last):
File "C:/Python34/hello world.py", line 7, in <module>
elif Side1*Side1+Side2*Side2==Side3*Side3 or Side3*Side3+Side2*Side2==Side1*Side1 or Side1*Side1+Side3*Side3==Side2*Side2:
TypeError: can't multiply sequence by non-int of type 'str'

This post has been edited by andrewsw: 18 December 2014 - 02:20 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is online

  • Unprocessable Entity
  • member icon

Reputation: 6594
  • View blog
  • Posts: 26,833
  • Joined: 12-December 12

Re: What's wrong with this code?

Posted 18 December 2014 - 02:19 PM

input() always returns a string, you need to use int() or float() to convert them to numbers (having checked that they are valid numbers of course).

It works for if Side1==Side2 and Side2==Side3 because you can compare strings, but you cannot multiply a string.

This post has been edited by andrewsw: 18 December 2014 - 02:20 PM

Was This Post Helpful? 0
  • +
  • -

#6 FlapperJacker   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-November 14

Re: What's wrong with this code?

Posted 18 December 2014 - 02:59 PM

View Postandrewsw, on 18 December 2014 - 01:19 PM, said:

input() always returns a string, you need to use int() or float() to convert them to numbers (having checked that they are valid numbers of course).

It works for if Side1==Side2 and Side2==Side3 because you can compare strings, but you cannot multiply a string.

Thanks it worked by changing them to floats
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1