I am writing an algorithm which should be able to determine in which quadrant a radian is based on two values that the user inputs. I think that the code is calculating the radian but I know that those values are not being compared to the pi values that I gave since I am not getting any output.
Code below:
print('Enter the radians of the angle (a*π/b): ')
a = int(input('a value(top): '))
b = int(input('b value(bottom): '))
radians = ((a*math.pi)/b)
print('')
print('Finding...')
if radians > 0 and radians < (math.pi/2):
print(f'The angle {radians}rad is in the I quadrant')
if radians > (math.pi/2) and degrees < math.pi:
print(f'The angle {radians}rad is in the II quadrant')
if radians > math.pi and radians < (3*math.pi/2):
print(f'The angle {radians}rad is in the III quadrant')
if radians > (3*math.pi/2) and radians < (2*math.pi):
print(f'The angle {radians}rad is in the IV quadrant')
CodePudding user response:
After correcting the typo of the second if-statement, I was also not getting any output on the console, until I made the following changes.
if radians > 0 and radians < (math.pi/2):
print(f'The angle {radians}rad is in the I quadrant')
elif radians > (math.pi/2) and radians < math.pi:
print(f'The angle {radians}rad is in the II quadrant')
elif radians > math.pi and radians < (3*math.pi/2):
print(f'The angle {radians}rad is in the III quadrant')
elif radians > (3*math.pi/2) and radians < (2*math.pi):
print(f'The angle {radians}rad is in the IV quadrant')
else:
print(f"The angle {radians}rad is on one of the axis.")
CodePudding user response:
First of all, you should rename degree to radians. The other problem is that you are assumed that all inputs are between 0 and 2pi. You should consider other inputs.
import math
print('Enter the radians of the angle (a*π/b): ')
a = int(input('a value(top): '))
b = int(input('b value(bottom): '))
radians = ((a*math.pi)/b)
print('')
print('Finding...')
while radians > 2 * math.pi:
radians -= 2 * math.pi
while radians < 0:
radians = 2 * math.pi
if 0 < radians < (math.pi / 2):
print(f'The angle {radians}rad is in the I quadrant')
if (math.pi / 2) < radians < math.pi:
print(f'The angle {radians}rad is in the II quadrant')
if math.pi < radians < (3 * math.pi / 2):
print(f'The angle {radians}rad is in the III quadrant')
if (3 * math.pi / 2) < radians < (2 * math.pi):
print(f'The angle {radians}rad is in the IV quadrant')
CodePudding user response:
Firstly, it is possible that you didn't write import math in your code too like you forgot here.
Secondly, if you input values that are on the axes, you won't get any output, since there is no condition for that. For instance, entering 3π/2 won't give any output since it is on an axis. P.S. Using elif after the first if statement would be better.
Here's my version of the code. Added import math and conditions to check whether the angle lies on an axis. This covers every possible input (except strings)
so there's no need to worry about 'No Output'.
Edit: Forgot to mention about the variable replacement of degrees to radians, since there is no variable named degrees.
import math
print('Enter the radians of the angle (a*π/b): ')
a = int(input('a value(top): '))
b = int(input('b value(bottom): '))
radians = ((a*math.pi)/b)
print('')
print('Finding...')
if radians > 0 and radians < (math.pi/2):
print(f'The angle {radians}rad is in the I quadrant')
elif radians > (math.pi/2) and radians < math.pi:
print(f'The angle {radians}rad is in the II quadrant')
elif radians > math.pi and radians < (3*math.pi/2):
print(f'The angle {radians}rad is in the III quadrant')
elif radians > (3*math.pi/2) and radians < (2*math.pi):
print(f'The angle {radians}rad is in the IV quadrant')
elif (radians == (3*math.pi/2)) or (radians == (math.pi/2)):
print(f'The angle {radians}rad is on Y axis.')
elif (radians == (math.pi)) or (radians == 0):
print(f'The angle {radians}rad is on X axis.')
Hope this help, if so, please accept this answer.
