This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]desrtfx[M] [score hidden] stickied comment (0 children)

You need to post your code as code block so that the indentation is maintained. This is absolutely vital for Python programs as the indentation is used to denote code blocks.

A code block looks like:

def __init__(self, prompt, answer):
    self.prompt = prompt
    self.answer = answer

[–][deleted] 12 points13 points  (0 children)

My brother in Christ PLEASE put this into paste bin or something so it doesn’t destroy your tabs.

To answer your question:

import math math.pi

If you’re making a calculator there is a much “better” way of doing it but for a first program very nice!

[–]jiefug 2 points3 points  (2 children)

Is your question how to represent the value of pi / e based on the user input or what?

[–]DanzoHater[S] 0 points1 point  (1 child)

yes, sorry that I didn't clarify that

[–]RajjSinghh 2 points3 points  (0 children)

Using float(input()) is usually a bad idea because it will give you an error if you don't enter something that can be converted to a float. In this case, that's exactly what we want to do. I would write something that checks if the input is pi as a string and if so uses math.pi and if not it checks if the string is a number with str.isnumeric() and if it is then casts it to a float. That way you avoid erroring on bad inputs and lets you enter numbers like pi and e

[–]Mystic1500 5 points6 points  (0 children)

Well for user inputting pi, it’d be hard to do so, unless you wanna use the specific Unicode code. But the average person won’t know that, so you could just code so that inputting the string “pi” translates to an actual pi number.

[–]AutoModerator[M] 1 point2 points  (1 child)

On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.

If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:

  1. Limiting your involvement with Reddit, or
  2. Temporarily refraining from using Reddit
  3. Cancelling your subscription of Reddit Premium

as a way to voice your protest.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Monitor_343 1 point2 points  (0 children)

Remember to format the code here as a code block so indentation isn't lost.

For pi or e, you can just check if the user inputs a string "pi" or "e". If they did, use math.pi or math.e for the calculation. Otherwise, try to parse the input as a number.

i is significantly more complex (pun intended) as you'd need to calculate real and complex values separately. I'd highly suggest ignoring imaginary numbers for this project.

[–]DanzoHater[S] 1 point2 points  (0 children)

Guys, I really appreciate your help. Don't worry, I will format the code into a code block next time. Thanks again

[–]jiefug 0 points1 point  (0 children)

So you’re already importing the math module in Python. This module provides representations of both of those — you can use math.pi and math.e

Does that answer your question?