Python:无法将弧度与 pi 值进行比较
Python: Not being able to compare radians with pi values
我正在编写一个算法,它应该能够根据用户输入的两个值确定弧度在哪个象限。我认为代码正在计算弧度,但我知道这些值没有与我给出的 pi 值进行比较,因为我没有得到任何输出。
代码如下:
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')
首先,您应该将度数重命名为弧度。另一个问题是您假设所有输入都在 0 到 2pi 之间。您应该考虑其他输入。
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')
首先,您可能没有在代码中写 import math
就像您忘记了这里一样。
其次,如果您输入轴上的值,您将不会得到任何输出,因为没有条件。例如,输入 3π/2
不会给出任何输出,因为它在一个轴上。 P.S。在第一个 if
语句之后使用 elif
会更好。
这是我的代码版本。添加了 import math
和条件来检查角度是否位于轴上。这涵盖了所有可能的输入(字符串除外)
所以不用担心 'No Output'.
编辑:忘记提及 degrees
到 radians
的变量替换,因为在第二个 if
条件中没有名为 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.')
希望对您有所帮助,如果有帮助,请采纳此回答。
更正第二个 if 语句的拼写错误后,我在控制台上也没有得到任何输出,直到我进行了以下更改。
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.")
我正在编写一个算法,它应该能够根据用户输入的两个值确定弧度在哪个象限。我认为代码正在计算弧度,但我知道这些值没有与我给出的 pi 值进行比较,因为我没有得到任何输出。
代码如下:
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')
首先,您应该将度数重命名为弧度。另一个问题是您假设所有输入都在 0 到 2pi 之间。您应该考虑其他输入。
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')
首先,您可能没有在代码中写 import math
就像您忘记了这里一样。
其次,如果您输入轴上的值,您将不会得到任何输出,因为没有条件。例如,输入 3π/2
不会给出任何输出,因为它在一个轴上。 P.S。在第一个 if
语句之后使用 elif
会更好。
这是我的代码版本。添加了 import math
和条件来检查角度是否位于轴上。这涵盖了所有可能的输入(字符串除外)
所以不用担心 'No Output'.
编辑:忘记提及 degrees
到 radians
的变量替换,因为在第二个 if
条件中没有名为 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.')
希望对您有所帮助,如果有帮助,请采纳此回答。
更正第二个 if 语句的拼写错误后,我在控制台上也没有得到任何输出,直到我进行了以下更改。
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.")