Program won't execute else command
print ('This program will determine if three angles can form a triangle')
print ('Enter Angle 1')
degree1=input()
print ('Enter Angle 2')
degree2=input()
print ('Enter Angle 3')
degree3=input ()
degrees= int(degree1+degree2+degree3)
if (degrees>180):
print ('Yes it does form a triangle as these angles are equal to 180')
else:
print ('No, it does not form a triangle as these angles are less than 180')
Printing Yes it does form a triangle as these angles are equal to 180 regardless of the numerical values entered.
If degree1,degree2 and degree3 are strings than en + between them would result in a string concentration not an addition. So 10 plus 10 plus 10 would result in a value "101010", converted to int, and then compared to 180.
You need to convert each degree input to an int before you add them up.
Related
Problem:
We know that the apothem "a", of a regular polygon with "n" sides, can be calculated from the side "s" by the following formula:
a = s / (2*tan(π/n))
Define the perimeterPentagon function that, given a non-negative value for the apothem, returns the perimeter of the respective regular pentagon, rounding the result to three decimal places.
For example:
Test:
print(perimeterPentagon(3))
Result:
21,796
Solution attempt:
import math
def perimeterPentagon(apothem):
if apothem > 0:
s = apothem*(2*math.tan(math.pi/5))
return (s/2*math.tan(math.pi/5))
else:
return []
What have I done wrong here?
I've come over this issue and after many hours of trying, I'd like to ask someone with more experience in python than me (which should be no problem because I am a python beginner).
There is my input.xyz file with eight points, which looks something like this:
15.486586, 46.798954, -6.232800, 15.445764, 46.807576, -6.249205, -0.040822,0.008622, -0.016405, 0.044832;
6.233575, 48.083754, -4.223557, 6.187027, 48.090785, -4.243389, -0.046548, 0.007031, -0.019832, 0.051083;
-2.159452, 40.818712, -3.104244, -2.200572, 40.818489, -3.120266, -0.041120,-0.000223, -0.016022, 0.044132;
45.554111, 131.689322, 1.525740, 45.452954, 131.721406, 1.483290, -0.101157,0.032084, -0.042450, 0.114298;
28.315109, 146.107918, 2.897549, 28.235633, 146.131800, 2.864060, -0.079476, 0.023882, -0.033489, 0.089489;
7.303209, 138.223347, 4.702106, 7.250850, 138.242379, 4.679564, -0.052359, 0.019032, -0.022542, 0.060098;
-32.211983, 148.909744, 12.919538, -32.279077, 148.907541, 12.876267,-0.067095, -0.002203, -0.043271, 0.079868;
-48.926024, 180.295215, 20.142896, -49.008547, 180.275117, 20.127614,-0.082523, -0.020098, -0.015282, 0.086299;
The ";" seperates every point and the first three values of one point are the x,y and z values. So I want to take three points with their xyz values und write them in a matrix with python.
This is what I got so far:
# creating empty list for append
xyz_matrx = []
counter = 0
for line in xyz:
counter += 1
# counter to get only first three columns
if counter%4 == 0:
break
# deleting whitespaces and selecting wanted data
linevalues = line.strip().split(",")
x = (linevalues[0:1])
y = (linevalues[1:2])
z = (linevalues[2:3])
xyz_matrx.append(x)
#flatten because xyz_matrix is a list within list
# values converting into float64, because string won't work for following
#work
flattenedx = [val for sublist in xyz_matrx for val in sublist]
matr_flatx = [float(i) for i in flattenedx]
A_matrx = mat(matr_flatx)
with this, I get a 1x3 matrix with the xyz points which are horizontal in the matrix, but I want three columns within the matrix, which stand for each point and rows which stand for xyz values, a 3x3 matrix in datatype float64.
If I change something with the indices, I only get string88 matrices.
I can create two more lists for the two other points, then I have three 1x3 matrices but ".append" won't work because I don't have a twodimensional matrix?
I know my code isn't very efficient but I hope somebody understood my problem and can help me.
Short: I've got an input .xyz file, only the first three values (x,y,z coordinates) of every point are relevant, I want three points of the xyz with each of their three coordinates in a 3x3 matrix (first column vertical: first point, xyz, second column vertical: second point with xyz and third column third point with xyz vertical down), the datatype has to be float64.
Here's one way you could do it
# creating empty list for append
xyz_matrix = []
counter = 0
with open('input.xyz', 'r') as f:
for line in f:
# Add the first three values to the matrix
xyz_matrix.append(map(float, line.strip().split(",")[0:3]))
counter += 1
if counter == 3:
break
# Transpose
xyz_matrix = zip(*xyz_matrix)
print xyz_matrix
You end up with a list of tuples, but that should be OK.
This is more direct, but less general
# Creating an empty 2D list
xyz_matrix = [[], [], []]
counter = 0
with open('input.xyz', 'r') as f:
for line in f:
# Add the first three values to the matrix
values = map(float, line.strip().split(",")[0:3])
# Append x, y, z to each rows 0, 1, 2 respectively
for i in range(3):
xyz_matrix[i].append(values[i])
counter += 1
if counter == 3:
break
print xyz_matrix
def bmi(weight,height):
"""bmi calculates anyones bmi.
weight=weight in pounds,height=someones height in inches,
this function returns the person's body mass index"""
weight_calc=weight*0.45 #converts weight to kilograms
height_calc=height*0.025 #converts height to meters
square=height_calc**2
calc=weight_calc/square #final bmi calculation
print 'For someone who is',height,'inches tall, and
weighs',weight,'pounds, their body mass index is',calc
def new_bmi():
"""asks someone their height in inches, and weight in pounds.Then
calcs bmi"""
tall=raw_input('How tall are you in inches?\n')
heavy=raw_input('How much do you weigh in pounds?\n')
print 'You are',tall,'inches tall, and weigh',heavy,'pounds.'
float(heavy)
bmi(heavy,tall)
new_bmi()
I have to write a program that asks someone their height in inches and their weight in pounds, and then uses that info to calculate their BMI using the bmi function. I keep receiving the error: 'can't multiply sequence by non-int of type 'float'"
thanks for any responses, they are much appreciated
In weight_calc=weight*0.45 the variable weight is a string from the user input, not an float or an int. You'll have the same problem with the line after it. At some point in your code you need to convert the weight and height to floats if you want decimal values, or int if you only want whole numbers. I suggest making the conversion when you call the bmi function:
#bmi(heavy,tall) #this is passing heavy and tall as strings, not numbers
bmi(float(heavy),float(tall)) #<-- use this instead
Output:
('For someone who is', 60.0, 'inches tall, and weighs', 160.0, 'pounds, their body mass index is', 32.0)
I have some code that will allow me to draw a crescent moon shape, and have extracted the values to excel to be drawn. However in place of some numbers, there is -1.#IND in place. Firstly if anyone could explain what this means, as Google came back with 0 links. And secondly if there is anyway to stop it from occurring.
There is a brief analogy of my code. I have lots more code besides this, however that is just calculating angles.
for(int j=0; j<=n; j++)//calculation for angles and output displayed to user
{
Xnew2 = -j*(Y+R1)/n; //calculate x coordinate
Ynew2 = Y*(pow(1-(pow((Xnew2/X),2)),0.5));
if(abs(Ynew2) <= R1)
cout<<"\n("<<Xnew2<<", "<<Ynew2<<")"<<endl;
}
MORE INFO
I'm now having the problem with this code.
for(int i=0; i<=n; i++) //calculation for angles and output displayed to user
{
Xnew = -i*(Y+R1)/n; //calculate x coordinate
Ynew = pow((((Y+R1)*(Y+R1)) - (Xnew*Xnew)), 0.5); //calculate y coordinate
AND
for(int j=0; j<=n; j++)//calculation for angles and output displayed to user
{
Xnew2 = -j*(Y+R1)/((n)+((0.00001)*(n==0))); //calculate x coordinate
Ynew2 = Y*(pow(abs(1-(pow((Xnew2/X),2))),0.5));
if(abs(Ynew2) <= R1)
cout<<"\n("<<Xnew2<<", "<<Ynew2<<")"<<endl;
I am having the problem drawing the crescent moon that I cannot get the two circles to have the same starting point? If this makes sense, I am trying to get two parts of circles to draw a crescent moon as such that they have the same start and end points. The only user input I have to work by is the radius and chosen center point.
If anyone has any suggestions on how to do this, it would be great, currently all I am getting more a 'half doughnut' shape, due to the circles not being connected.
#IND means an indetermined form.
What you have there is something known as 'Not a number' or NaN for short.
Quoting from Wikipedia, generation is done by:
Operations with a NaN as at least one operand.
The divisions 0/0 and ±∞/±∞
The multiplications 0×±∞ and ±∞×0
The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions
The square root of a negative number.
The logarithm of a negative number
The inverse sine or cosine of a number that is less than −1 or greater than +1.
You're doing at least one of those things.
Edit:
After analyzing your code, these are 2 possibilities:
When n == 0 in the first iteration where j == 0 too, Xnew2 will be -1.#IND
When Xnew2 is greater than X, Ynew2 will be complex -> NaN
You are doing something illegal to a floating point number, such as taking the square root of a negative number. This is presumably on Windows. On Linux, you would get NaN (not a number) or inf. See -1 #IND Question for further information; the link provided in the second answer is helpful.
This from the Wikipedia entry for IEEE 754 Nan:
There are three kinds of operations that can return NaN:
Operations with a NaN as at least one operand.
Indeterminate forms
The divisions 0/0 and ±∞/±∞
The multiplications 0×±∞ and ±∞×0
The additions ∞ + (−∞), (−∞) + ∞ and equivalent subtractions
The standard has alternative functions for powers:
The standard pow function and the integer exponent pown function define 00, 1∞, and ∞0 as 1.
The powr function defines all three indeterminate forms as invalid operations and so returns NaN.
Real operations with complex results, for example:
The square root of a negative number.
The logarithm of a negative number
The inverse sine or cosine of a number that is less than −1 or greater than +1.
You are raising a negative number to the power of a non-inverse (i.e. 1/2, 0.5, 0.25, 0.333333, etc.) which results in a complex number. Like sqrt(-1) aka (-1)^(0.5)
Additionally you could also be equating 0/0 in two of your lines of code.
Use this code instead: (It takes the absolute value of your power's base (preventing negative values, thus preventing imaginary answers (complex numbers = NaN = -1.#IND)) It also prevents you from dividing by 0 if n == 0... in this event it adds 0.00001 to the denominator
for(int j=0; j<=n; j++)//calculation for angles and output displayed to user
{
Xnew2 = -j*(Y+R1)/((n)+((0.00001)*(n==0)); //calculate x coordinate
Ynew2 = Y*(pow(abs(1-(pow((Xnew2/X),2))),0.5));
if(abs(Ynew2) <= R1)
cout<<"\n("<<Xnew2<<", "<<Ynew2<<")"<<endl;
}
{
Xnew3 = -j*(Y+R1)/((n)+((0.00001)*(n==0)); //calculate x coordinate
Ynew3 = Y*(pow(abs(1-(pow((Xnew3/X),2))),0.5)); //calculate y coordinate
if(abs(Ynew3) <= R1)
cout<<"\n("<<Xnew3<<", "<<Ynew3<<")"<<endl; //show x,y coordinates
}
*In the future avoid taking roots of negative numbers (which is the same as raising a negative number to a non-inverse-fraction power), avoid taking a logarithm of a negative number, and avoid dividing by 0 these all produce NaN (-1.#IND)
This code may be better (it uses conditional values to make your power's base zero if it is ever less than zero to prevent imaginary answers):
for(int j=0; j<=n; j++)//calculation for angles and output displayed to user
{
Xnew2 = -j*(Y+R1)/((n)+((0.00001)*(n==0)); //calculate x coordinate
Ynew2 = Y*(pow(((1-(pow((Xnew2/X),2)))*((1-(pow((Xnew2/X),2)))>(0))),0.5));
if(abs(Ynew2) <= R1)
cout<<"\n("<<Xnew2<<", "<<Ynew2<<")"<<endl;
}
{
Xnew3 = -j*(Y+R1)/((n)+((0.00001)*(n==0)); //calculate x coordinate
Ynew3 = Y*(pow(((1-(pow((Xnew3/X),2))))*((1-(pow((Xnew3/X),2))))>(0))),0.5)); //calculate y coordinate
if(abs(Ynew3) <= R1)
cout<<"\n("<<Xnew3<<", "<<Ynew3<<")"<<endl; //show x,y coordinates
}
* I'd also like to point out what "Magtheridon96" mentioned in his answer. The code now makes sure n is not equal to zero otherwise you could be dividing by zero, although I think that would produce #INF not #IND... unless "-j(Y+R1)" is also zero, then it will be 0/0 which will be #IND
I have 2d map and I want to check if line collides with any element. I need a function that can tell me if the line intersect any object on its way.
Take a look:
Red lines are not correct (function should return false), and green are (return true).
My collision map is map of boolean with 1 for wall and 0 for empty space.
How to this? I've read that I need to check if the line intersect any wall, but i have completely no idea how to do this on 2d map.
Thanx for any replies.
It depends on how your walls are represented.
If they are rectangle, then look for the line/segment intersection between your line and the 4 segments representing the rectangle.
If they are pixels, you can use the bresenham line algorithm to see if the pixels on the line are on these blocks.
If your walls are represented as line-segments you could test for intersection of line-segments as Paul Bourke describes: http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
If your walls are represented as polygons you could clip your test line against the wall polygon and see if the clipping result is non-empty as Paul Bourke describes: http://local.wasp.uwa.edu.au/~pbourke/geometry/cliplinetopoly/
So, I imagined that your cells are squares... let say they're unit squares. So, if I have any coordinate, for a point like , which are floats or doubles, they are in cell .
you need to know in which cells are the endpoints of the line
walk straight from one endpoint to the other and for each cell, test if it's a wall or not
return false when a wall is found, true otherwise.
To walk from one endpoint to the other, you need to compute the delta of each axis (call delta_x and delta_y, those values are 'int' because we're talking cells here), i.e. the number of cells it goes on the vertical and on the horizontal. You take the largest of the two. You will use that largest value for your loop. Let say it's this value is D = max(delta_x, delta_y) and XX and YY are the coordinate of the cell of one endpoint.
float step = 1.0f / D;
float current_location = 0.0;
for (int i = 0; i <= D; ++i, current_location += step)
{
int cur_x = XX + current_location * delta_x;
int cur_y = YY + current_location * delta_y;
if (intersect_wall(cur_x, cur_y))
return false;
}
return true;
That's it... adapt this to your functions.