Ask coding questions

← Back to all posts
Python
ThuwarahanNant1 (0)

I tried to make a 'calculator' to help with my GCSE Astronomy course at school. I have got it to work but I think that there should be an easier way to carry this out without using lots of lines of code. So far, I have made it so that each calculation is carried out with a function that takes certain parameters that are inputted by the user. Then, all the different calculations are outputted on the terminal and the user enters the corresponding number. Each function is assigned a number and is then carried out.
I really want to know how I can make the code more efficient and readable and whether any of the calculations are incorrect. Please help!!

Answered by malvoliothegood (147) [earned 5 cycles]
View Answer
Commentshotnewtop
malvoliothegood (147)
  • I think your code is pretty good
  • Maybe, your functions could return a value from the calculation they are doing, then you could print it.
  • You could put your instructions in a function (called instructions?) and call it.
  • You could put the if/elseif/if etc. in a function (called menu?)
  • You could return early after doing a calculation in the menu function
  • Have a look at this: https://repl.it/@malvoliothegood/Astronomy-Calculations
ThuwarahanNant1 (0)

@malvoliothegood Thank you so much. I really appreciate it. Also, is there a way to keep repeating the instruction and menu functions until the user enters something like "quit"?

malvoliothegood (147)

@ThuwarahanNant1
Here is a link to an example program that shows how to do what you are asking for. It is a bit more complicated as it tests user input to see that it is valid. You may not want or need to do this in your program, but in reality you would have to do this in the "real world". I used to teach programming at High School and required my students to use error checking in their programs: https://repl.it/@malvoliothegood/menu-example