Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculating length of side of the triangle

Hi I have problem that says:

PS C:\Users\root\Documents\lpthw> python .\ex3.1.py
Podaj długość: 5
Który bok: przypdl, przypkr, przec: przec
Traceback (most recent call last):
  File "C:\Users\root\Documents\lpthw\ex3.1.py", line 22, in <module>
    find(dl)
  File "C:\Users\root\Documents\lpthw\ex3.1.py", line 13, in find
    przypkr=x/2
TypeError: unsupported operand type(s) for /: 'str' and 'int'

My code:

import math
a=0
b=0
x=0
c="abc"
przypdl=0
przypkr=0
przec=0
dl=input("Podaj długość: ")
def find(x):
    c=input("Który bok: przypdl, przypkr, przec: ")
    if c=="przec":
        przypkr=x/2
        przypdl=przypkr*math.sqrt(3)
    elif c=="przypkr":
        przypdl=x*math.sqrt(3)
        przec=x*2
    else:
        przypkr=x/math.sqrt(3)
        przec=przypkr*2
    print(f'przeciwprostokątna: {przec}, krótsza przyprostokątna: {przypkr}, dłuższa przyprostokątna: {przypdl}')
find(dl)

Im starting with coding so that will be realy helpfull, thanks!
The purpose of my code is calculating length of side of the triangle.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The input you are getting is a string. You have to convert it to an int first.

dl=int(input("Podaj długość: "))

You also have to make przypkr, przec, and przypdl global because you are referencing those variables in a function. Adding these three lines in the beginning of the function should fix that.

global przypkr
global przec
global przypdl
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading