Python Forum
[split] simple calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[split] simple calculator
#2
You've made a good start.

For functions, get in to the practice of using doc strings, like this:

def add(n1, n2):
    """
Takes two floating point numbers and returns the sum
    """
That way the text can be extracted with:

print(add.__doc__)
... add, in the above example being the function name.

Personally, I wound not have the main body of the code as a function, rather have it as a body of code in its own right, with all the functions above the main body.

The next thing to do is to sanitize the user input. As an example, if a user does not enter a number, have your script catch that and once again ask for a number. This can be done without using try / except. If you want me (or anyone else) to show you how that can be done, then fair enough; just ask, but you'll learn a good deal more if you can work that out for your self. Hint: write a function to check the user input before the rest of your script accepts it.

I'll not present you with a large list of things to do right now, but there are other improvements that can be made, such as taking all of the input in one hit, then extracting the elements so that the calculation can be made.

Hint: run this...

calculate = list(input(": "))
print(calculate)
... and enter 7*4
FelixLarry likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply


Messages In This Thread
[split] simple calculator - by FelixLarry - Aug-18-2022, 02:24 PM
RE: [split] simple calculator - by rob101 - Aug-18-2022, 03:38 PM
RE: [split] simple calculator - by FelixLarry - Aug-18-2022, 04:07 PM
RE: [split] simple calculator - by rob101 - Aug-18-2022, 04:42 PM
RE: [split] simple calculator - by FelixLarry - Aug-19-2022, 12:41 AM
RE: [split] simple calculator - by menator01 - Aug-19-2022, 06:29 AM
RE: [split] simple calculator - by rob101 - Aug-19-2022, 10:07 AM
RE: [split] simple calculator - by FelixLarry - Aug-19-2022, 11:15 PM
RE: [split] simple calculator - by perfringo - Aug-19-2022, 10:46 AM
RE: [split] simple calculator - by FelixLarry - Aug-20-2022, 11:34 AM
RE: [split] simple calculator - by Yoriz - Aug-19-2022, 01:22 PM
RE: [split] simple calculator - by rob101 - Aug-20-2022, 12:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  a simple calculator Solstice 17 9,083 Mar-10-2019, 09:15 AM
Last Post: Ablazesphere

Forum Jump:

User Panel Messages

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