Sunday, September 15, 2024

Numworks: Allowing Repeated Calculations in Python

Numworks: Allowing Repeated Calculations in Python



Introduction


Say we want the user to repeat a calculation or a routine for as long as the user wants. At the end of each calculation, the user is asked if they want another calculation. The user enters one of two possibilities:


1 for an additional calculation

0 for no more calculation


A flag variable is set up with a default value of 1. As long as this value remains at 1, the calculation repeats.


Scrip structure (I named this variable rptflag, but it can be any name):


rptflag=1


while rptflag!=0:

  …

  < insert inputs and show results >

  …

  print(“Again? No = 0, Yes = 1”)

  rptflag=eval(input())


Entering 0 for the Again? Question exits the loop. We need the eval (evaluate) or int (input) because leaving the input command alone defaults the input to a string.


For the sample scripts below, the escape velocity off the surface of planets and other celestial objects are calculated.


Repeated Loop: General


This script does not require any special modules.


Script: rptdemo1 (We can use this on any calculator or platform)


from math import *


# repeat demonstration 

# set up repeat flag

rptflag=1


while rptflag!=0:

  print("ESCAPE VELOCITY")

  m=eval(input("Mass (kg)? "))

  r=eval(input("Radius (m)? "))

  v=sqrt(1.33486e-10*m/r)

  print("Velocity: ",v,"\nm/s")

  print("Again? no=0, 1=yes")

  rptflag=eval(input())




Repeated Loop: Numworks – using the keyboard


Instead of having the user enter 0 or 1, the user presses the keys 0 or 1. To allow the user key presses, use the Ion module. KEY_ZERO is for the 0 key and KEY_ONE is for the 1 key.


To make the screen look presentable, I’m clearing the screen. This is accomplished by the use of the Kandinsky module. The use of the Kandinsky module was suggest to me on Reddit by Feeling_Walrus3033, and I give credit and gratitude for the suggestion.


The line:


fill_rect(0,0,320,240,(255,255,255)) creates a blank white screen.


Clearing the screen this way will call for a way to display text on the screen. Unfortunately the print() command won’t do it. Kandinsky comes to the rescue with the draw_string command. The syntax for the draw_string command is:


draw_string(“text” or variable containing the string, x pixel, y pixel, [text color], [background color])


Colors are optional. The default is black text on white background.


Keep in mind that using this method will require more memory.


Script: rptdemo2 (Specific to Numworks, for other platforms and calculator, check your specific manual)


from math import *

from ion import *

from kandinsky import *

# repeat demonstration 


# set up keys

def key():

  while True:

    if keydown(KEY_ZERO):

      return 0

    if keydown(KEY_ONE):

      return 1


# set up repeat flag

rptflag=1


while rptflag==1:

  print("ESCAPE VELOCITY")

  m=eval(input("Mass (kg)? "))

  r=eval(input("Radius (m)? "))

  v=sqrt(1.33486e-10*m/r)

  

  # build strings

  s1="Velocity: "+str(v)+" m/s"

  s2="Again? no=0, 1=yes"

  fill_rect(0,0,320,240,(255,255,255))

  draw_string(s1,0,0)

  draw_string(s2,0,20)

  rptflag=key()


draw_string("DONE",0,40,(255,0,0))



Sample Data to Try



Mass (kg)

Radius (m)

Escape Velocity (m/s)

Earth

5.972168E24

6378.137E3

11179.88618486758

Jupiter

1.8982E27

71492E3

59533.32250562

Sun (Solar System)

1.9885E30

6.957E8

617688.6988877566


E: [ ×10^x ] button, shown as “e”



Note: I will be in Nashville for the 2024 HP Handheld Conference on September 21-22, 2024. The next scheduled post will be on September 28, 2024.


Take care,


Eddie


All original content copyright, © 2011-2024. Edward Shore. Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited. This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.



TI 84 Plus CE: Consolidated Debts

TI 84 Plus CE: Consolidated Debts   Disclaimer: This blog is for informational and academic purposes only. Financial decisions are your ...