![]() ![]() Result_format="\nThe volume of the sphere is: units.", "Enter the radius of the circle in the units of your choice: " This could be another lambda in the data, perhaps attached to each prompt, so that you can configure it to ask for integers or strings instead of just floats. For example, float(input(prompt)) is "hardcoded" in the logic. Instead, the data structure dictates how many variables to ask for, how to transform them into an answer, and how to format the answer for display.įrom here, you could generalize this in all kinds of ways with few changes to the code. Observe how the actual code has almost nothing to do with what you're calculating, or how, or what the output will look like. It is natural to group those formulae alongside the other traits of the calculation. Functions can be dataĮach kind of user-selected computation has a formula to go with it. If you translate your program to another language, it won't work anymore. Use data structures that group input characteristics together, keeping display values independant from logic-affecting values.Īs written, the button labels dictate the program flow ( if var.get() = "Circle Circumference": etc.). This is the same point as the previous one, just applied to input instead of output. It's better if those functions return numbers to be printed later by formatter function. Think about the task of using your code to make a web page: it's going to be hairy because your math functions have print statements in them. Do this by keeping results in a structure that preserves the semantics of the data, and defer formatting to the very end. Make it easy to affect how output is displayed. Not only do you have to change the code, but it's been months since you looked at it! You won't remember how everything works. Separate content from presentationĪ common pattern arises when you create something that's useful to someone (yourself, usually), later it becomes useful to other people, and those people need the output in a different format. Define pi with more digits, and shorten the result so that it shows the actual precision. You're displaying results with maximum "precision" but basing them on a value of pi that has only four significant figures. Especially here, where the change will affect the output in surprising and subtle ways. This is bad! Mangling user data is necessary sometimes and to be avoided otherwise. You're silently truncating the user's input with int(). In that light, here are things I'd do differently from what you have done. broadness of their working definition of the word data.organization of data in a manner that lets the data drive the program's flow.To me, the most obvious differences between experienced and novice programmers are: ![]() I think it would be instructive to compare my rank amateur code to the code that an experienced software developer would produce to accomplish the same result. R = int(input("Enter the radius of the sphere in the units of your choice: "))īutt1 = ttk.Button(root, text = "OK", command = main)īid(row = 2, column = 1, padx = 5, sticky = W)īutt2 = ttk.Button(root, text = "Exit", command = exit)īid(row = 6, column = 0, padx = 5, pady = 5, sticky = W) ![]() R = int(input("Enter the radius of the circle in the units of your choice: ")) Print("\nThe volume of the sphere is: " +str(4/3 * pi * r**3) + " units.") Print("\nThe area of the circle is: " + str(pi*r**2) + " units.") Print("\nThe circumference of the circle is: " + str(2*pi*r) + " units.") #txt_data_in.grid(row = 5, column = 0, padx = 5, sticky = W) #txt_data_in = Text(root, height = 1, width = 30, bg = "Light Grey") id(row = 4, column = 0, columnspan = 3, sticky = EW) # Note separator sticky EW! Line1 = ttk.Separator(root, orient = HORIZONTAL) "Rectangle Area", "Rectangle Prism Volume", "Cone Volume")īox.grid(row = 2, column = 0, padx=5, pady=5, sticky= W) Lbl_id(row = 1, column = 0, padx = 5, pady = 0, sticky = W)īox = ttk.OptionMenu(root, var, "Circle Circumference", "Circle Area", "Sphere Volume", Lbl_choose = Label(root, text = "Choose a function and hit OK:") Lbl_id(row = 0, column = 0, columnspan = 2, padx =5, pady = 10) Lbl_title = Label(root, text = "GEOMETRIC SHAPES AND VOLUMES") Essentially, I think it would be instructive to compare my rank amateur code to the code that an experienced software developer would produce to accomplish the same result. ![]() I have a working piece of code for calculating area and volume of different geometric shapes, using user input of "radius".Īs I have said, the code works but I would like to know how I can improve it to be shorter, faster and more efficient. I'm a beginner coder, doing it solely for fun, having started coding about two months ago with Python. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |