This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 1 year ago.
Trying to find the area of a triangle, the code is pretty simple but whenever I run the code nothing happens, not even the first line of print("width of the base").
print("width of the base")
width = input()
print("height")
height = input()
variable1 = width*height
area = variable1/2
print("area = {0}".format(area))
What you are doing will only work for a right angled triangle, regardless, i agree with #BuddyBoblll that you need to type a number.
Instead of using the (height*base/2) formula, you can use the Heron's formula, which will need only one or two additional lines of code. Furthermore, it will find area for all types of triangles, and is not restricted to right angled ones.
# Three sides of the triangle is a, b and c:
a = float(input('Enter first side: '))
b = float(input('Enter second side: '))
c = float(input('Enter third side: '))
# calculate the semi-perimeter
s = (a + b + c) / 2
# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
The output that i am getting for this code is:
Enter first side: 5
Enter second side: 12
Enter third side: 13
The area of the triangle is 30.00
Side note, i am working on mac osx on a Python 3.8.0 version.
Related
I have written this program which asks the user about how many rectangles they wants to print out. It also asks for the width and height of each, and prints the triangles. After asking the height and width of each, it moves onto the next rectangle and so on.
This all works fine using the program I've made but at the end I want to print out the total area of all the rectangles that the user has created. How can I update my code and achieve this? How is it possible to store the area of the first rectangle and add the area of the second to this first area and so on?
Here is the code:
size = input("How many rectangles?" ) #asks the number of rectangles
i=1
n = 1
while i <= size:
w = input("Width "+str(n)+"? ") #asks for width of each rectangle
h = input("Height "+str(n)+"? ") #asks for height of each rectangle
n=n+1
h1=1
w1=1
z = ""
while w1 <= w:
z=z+"*"
w1+=1
while h1<=h:
print z
h1+=1
i+=1
How about you just accumulate the total area?
Above your loop, do:
area = 0
Then, somewhere inside your loop, after you've got w and h from the user, just do
area += w * h
When you finish looping, area will contain the total area.
This code should really use a for loop instead of a while loop to keep track of counters, keep numbers in variables instead of just "*" strings, and use += instead of x=x+1 in a few places, among other things, but here's a minimal step to solve the total area problem you specifically asked about:
size = input("How many rectangles?" ) #asks the number of rectangles
i=1
n = 1
area = 0
while i <= int(size):
w = float(input("Width "+str(n)+"? ")) #asks for width of each rectangle
h = float(input("Height "+str(n)+"? ")) #asks for height of each rectangle
n+=1
h1=1
w1=1
z = ""
while w1 <= w:
z=z+"*"
w1+=1
while h1<=h:
print(z)
h1+=1
area += len(z)
i+=1
print('total area = ',area)
I'm writing a code for my class but I'm having a little trouble at one part. I'm having the user input a number and then I need a loop to print specific statements based off the number the user inputted. So for example:
def main():
totalnumber = input("Enter the number of circles: ")
i = 0
for i in totalnumber:
i = 0 + 1
value = input("Enter the radius of circle",str(i)+":")
So I basically need the output to look like:
Enter the number of circles: 3
Enter the radius of circle 1:
Enter the radius of circle 2:
Enter the radius of circle 3:
I'm getting the error
TypeError: input expected at most 1 arguments, got 2
Is what I'm doing above okay to do or should I use a different approach?
If its okay what is wrong within my code that would be giving me that sort of error?
Try:
def main():
total_number = input("Enter the number of circles: ")
for number in range(1, int(total_number) + 1):
value = input("Enter the radius of circle {}: ".format(number))
main()
First: you need to convert the input to int, then iterate it by the number.
Notes:
use python pep-8 when naming your parameters user _ between the names
string formating is best with format try to use it.
Your for loop doesn't look correct.
Try
for number in range(int(totalnumber)):
i = number+1
value = input("Enter the radius of circle"+str(i)+":")
So today in science class I thought of making a python script for basic perimeter of a quadrilateral. Later in future I want to expand into circle and other shape but I got stuck in error. Please help.
My code:
print ("This is a program to find the perimeter of a quadrilateral. Input the length and breath and get the desired perimeter")
len = input("What is the length?")
bre = input("What is the breath?")
length = len + len
breath = bre + bre
perimeter = length + breath
print ("The perimeter of the quadrilateral is :" + perimeter)
https://repl.it/xHG
And the output comes funky. If l=2 and b=1 then output comes as 2211.
Also, how do you expand it into different shapes? I was thinking of using if and else options so if choice = circle then execute circle code elif if choice = triangle then execute triangle code. Does anyone have a better idea?
You need to convert you input to an int or float.
len = float(input("What is the length?"))
In your code
len = input("What is the length?")
len is a string, and therefor when you perform len + len you are performing String concatenation
Remember to convert data types
print ("This is a program to find the perimeter of a quadrilateral. Input the length and breath and get the desired perimeter")
len = input("What is the length?")
bre = input("What is the breath?")
len=int(len)
bre=int(bre)
length = len + len
breath = bre + bre
perimeter = length + breath
print ("The perimeter of the quadrilateral is :" + str(perimeter))
In Python 3, input returns a string (this is different in Python 2.x, which may be part of the confusion).
That means that length = len + len is actually performing string concatenation, ie. '2' + '2' = '22'.
Using either int(input("...")) or float(input("...")) will turn them into numbers. (note that both functions will create errors if the user puts in strings that can't be converted to numbers.
I have problems with my program that I have to do... here are the instructions: Write a program named triangle.py that uses a value-returning function that takes the lengths of the sides of a triangle as arguments and returns both the area and perimeter of the triangle. Prompt the user to enter the side lengths from the keyboard in the main function and then call the second function. Display the area and perimeter accurate to one decimal place. NOTE: Use Heron's Formula to find the area. I did it and it worked but the thing is he wants us to use one function that returns both area and perimeter.. so I re-did the program using one function but I keep getting an error saying per is not defined... I tried literally everything to fix it, looked online and nothing works. :( here is my code:
def main():
a = float(input('Enter the length of the first side: '))
b = float(input('Enter the length of the second side: '))
c = float(input('Enter the length of the third side: '))
print('The perimeter of the triangle is: ', format(per(a, b, c),',.1f'))
print('The area of the triangle is: ', format(area(a, b, c),',.1f'))
def area_per(a, b, c):
per = a + b + c
s = per / 2
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
return area_per
main()
The problem here is that the "per" variable you defined is local to the area_per function.
You'd need to create another function per that stands outside of that if you want the right scope.
def main():
a = float(input('Enter the length of the first side: '))
b = float(input('Enter the length of the second side: '))
c = float(input('Enter the length of the third side: '))
print('The perimeter of the triangle is: ', format(per(a, b, c),',.1f'))
print('The area of the triangle is: ', format(area_per(a, b, c),',.1f'))
def area_per(a, b, c):
p = per(a,b,c)
s = p / 2
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
return area
def per(a,b,c):
return a+b+c
main()
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
How do you write a program that works out the area of a rectangle, by collecting the height and width of the rectangle from the user input, calculates the area and displays the result? How would you do it with the volume for a cuboid as well? My code so far is: (I've just started python)
shape = input("> ")
height = input("Please enter the height: ")
width = input("please enter the width: ")
area = [height*width]
print ("The area is", 'area')
But I am receiving invalid syntax.
In Python 3.x, input returns a string. So, both height and width are strings.
area = [height*width]
You are multiplying strings here and creating a list. You need to convert them to either integers (with int function) or floating point numbers (with float function), like this
height = float(input("Please enter the height: "))
width = float(input("please enter the width: "))
...
area = height*width
And then, its better to pass a single string to the print function, like this
print ("The area is {}".format(area))
Or you can simply print the items like this
print ("The area is", area)
print ("The area is", area)
and you don't need to store the area in a list - area = height * width is enough.
Just do it similarly to calculate the volume of a cuboid:
l = int(input("Please enter the length: "))
h = int(input("Please enter the height: "))
w = int(input("please enter the width: "))
vol = l*h*w
print ("The volume is", vol)
Notice that you need to convert the user input to an int before trying to do any math with them.
make sure that the user is only able to enter floats and not strings else you will get an error:
height = float(input("What is the height?")
once you have both of the inputs then to output:
area = height * width
print("The area is{0}".format(area))