Python Forum
Basic Trigonometry Function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Trigonometry Function
#1
I know I just recently posted a thread, but these are the only two programs i'm having trouble with trying to get them to run properly. I will include the assignment and code I have below, thanks guys!!!!

For this assignment, you need to create a program that allows the user to do basic trigonometry functions. First, ask the user if he or she would like to do sine, cosine, or tangent (sin, cos, tan). Then, ask the user for a decimal number and return the results.

print("Basic Trigonometry functions")
import math
trigfunction = input("Choose either sin, cos, tan:")
x = float(input("enter number:"))
sin = float sin
cos = float cos
tan = float tan
if sin():
    return mathsin(x)
while true:
        if cos():
            return(mathcos(x)
while true:
         if tan():
            return(mathtan(x)
Reply
#2
Please don't do everything the same time. Do it step by step.
First: Look at you post. Does it look good? Only the first line of your code is well formatted like Python code. You misplaced the tag to end the Python code. Move it from the end of the first line of code to the end of the last line of code and check it with "Preview Post".

Then: what do you expect from this line:
sin = float sin
First: when you are using the sin() function the argument must be between brackets like this:
sin = float(sin)
But that would not help you. Where did you introduce a variable with the name sin? You did not. So far you only have trigfunction and x.
First think what you really want.
Reply
#3
The error message will help you fix the issues. If you don't understand the error, feel free to share it and we'll help explain. But just dropping something that would be throwing 10+ errors and expecting us to explain every one of them for you is a little ridiculous lol
Reply
#4
Its not that i'm trying to make it difficult on people trying to help, it just that this stuff is due Wednesday and i'm not sure what more I need to do to get this program workin. So, I'm hoping someone can write the correct version of the program and I could understand that way because i'm running out of time.
Reply
#5
(Dec-13-2019, 09:56 PM)m8jorp8yne Wrote: So, I'm hoping someone can write the correct version of the program and I could understand that way because i'm running out of time.
This is not homework writing service. We are glad to help, but will not do your homework for you.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Well if that's the case, people who wants to make jokes like it's funny and not trying to help like the last guy who replied, shouldn't even respond. I'm still a beginner at this and I need guidance to understand before my deadline. That is all I'm trying to do.
Reply
#7
It's not about making funny. It's forum rules about homework questions.
You were explicit that you hope someone will write the code for you because the deadline is on Wednesday. Your deadline is none of our concern.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
I myself when starting to make programs found it very helpful to use Nassi–Shneiderman diagram. When I had to make a program like you I first made a structure diagram like this:
[Image: TRIGO.png]
You read this NSD from top to bottom. A rectangle denotes an action so the program starts with asking the function to be applied. An upside-down triangle denotes a choice. So the whole program consists of two input statements and one if ... elif ... else construction.
Your program starts indeed with the two input statements, you did that right. After that you have two variables: "trigfunction" and "x".
Then you do something with variables you do not have. Delete these 3 lines.
Then you start correctly with an if statement. But why do you continu with while? You should use elif.
I hope this helps you.
Reply
#9
OK, I sympathize. The code here obviously cannot be submitted as is, but should get you going in the right direction.
import math #this line is good
trigfunction = input("Choose either sin, cos, tan:") #great, but you never use the value trigfunction
x = float(input("enter number:")) #good
#sin = float sin    These lines are meaningless
#cos = float cos
#tan = float tan
if trigfunction == 'sin':
    print(math.sin(x)) #recognize this is in radians. Note that since you are not in a function "return" does not work
#while true: This is a wtf. You are purposefully creating an unending loop
elif trigfunction == 'cos':
    print(math.cos(x))
# while true: again, wtf?
         #if tan(): Do the tan like the sin and cos above
            #return(mathtan(x)
Reply
#10
I'll have a go at this problem, it looks quite fun:
import math

userNumber = input("Please enter the number that you want to use: ")
while True:
    try:
        uNF = float(userNumber)
        break
    except ValueError:
        userNumber = input("Sorry, you input an invalid number. Please try again: ")
while True:
    chosenFunction = input("Would you like to use sin, cos or tan? ").lower().replace(" ","")
    if chosenFunction == "sin":
        print('The sine of ', uNF, ' is ', math.sin(uNF), ".")
        break
    elif chosenFunction == "cos":
        print('The cosine of', uNF, 'is', math.cos(uNF), ".")
        break
    elif chosenFunction == "tan":
        print('The tangent of ', uNF, ' is ', math.tan(uNF), ".")
        break
    else:
        print("Sorry, you input an incorrect response.")

Oops, I made a mistake in my code: the calculation was in radians and not degrees. Here is a fixed version:
import math

userNumber = input("Please enter the number that you want to use (in degrees): ")
while True:
    try:
        uNF = float(userNumber)
        break
    except ValueError:
        userNumber = input("Sorry, you input an invalid number. Please try again: ")
uNFO = uNF
uNF = math.radians(uNF)
while True:
    chosenFunction = input("Would you like to use sin, cos or tan? ").lower().replace(" ","")
    if chosenFunction == "sin":
        print('The sine of ', uNFO, ' is ', math.sin(uNF), ".")
        break
    elif chosenFunction == "cos":
        print('The cosine of', uNFO, 'is', math.cos(uNF), ".")
        break
    elif chosenFunction == "tan":
        print('The tangent of ', uNFO, ' is ', math.tan(uNF), ".")
        break
    else:
        print("Sorry, you input an incorrect response.")
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020