-1

I am unable to generate a random triangle inside the 5x5 square. Then find the area and perimeter of that random triangle.

I have to use basic python so below is what code I have tried.

from random import *
from math import *

#ask the user for how many triangles they want to test
trials = int(input("How many random triangles would you like to test? "))

#Create the 5x5 square
square = []
for length in range(5):
    square.append([])
for width in range(5):
    square[length].append("0")

for triangle in range(trials):
    x = randint(0, len(square)-1)
    y = randint(0, len(square[0])-1)

I am looking for the code that will allow a random triangle to be plotted within the square.

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • The common way to display things on the screen in Python 3.x is to use the built-in [`print()`](https://docs.python.org/3/library/functions.html#print) function. – martineau Nov 07 '19 at 19:44
  • You haven't included anything that displays output: `print`, or a plot, or any such thing. You do some computations and the print without any return communication. – Prune Nov 07 '19 at 19:44
  • My apologies, i reworded the question. – pythonNoob1234 Nov 07 '19 at 19:53
  • 1
    Plotting isn't really basic python functionality. Which library do you want to use? – JohanC Nov 07 '19 at 21:16

1 Answers1

0

You are missing a print command in the for loop. Whatever you want to print, put as print(x, y) ( or print x,y ) depending if you are using python 3 or 2 respectively.

Look into matplotlib and search for some examples how to plot a line between to (x, y) vectors. That should be it.

Here's an example Plotting lines connecting points

El Dude
  • 5,328
  • 11
  • 54
  • 101