User:Guy vandegrift/S/2

From Wikiversity
Jump to navigation Jump to search


import random, math
from math import log10
def whatis(string, x):
    print(string+' value=',repr(x),type(x))
    return string+' value='+repr(x)+repr(type(x))
def roundSigFig(x, sigFig):
    #rounds positive or negative x to sigFig figures
    from math import log10, floor
    return round(x, sigFig-int(floor(log10(abs(x))))-1)
class QuesVar:
    #creates random input variables x.v(t) is number(text)
    def __init__(self, first, low, high, firstRendition):
        sigFigQues=3
        self.precision=3 #Number of digits to round
        self.first=first
        # see if either low or high are floating
        oneFloats=isinstance(low,float) or isinstance(high,float)
        if firstRendition:
            temp=first #temp is the local variable for self.v
            self.v=temp #v=value
        else:  #it is not the first question and may or may not float
            if oneFloats: #select a random floating value
                temp=random.uniform(low,high)
                temp=roundSigFig(temp,sigFigQues)
                self.v=temp
            else:
                temp=random.randint(low,high)
                temp=roundSigFig(temp,sigFigQues)
                self.v=temp
        #tempV=self.v and is used to create self.t (text version
        if abs(temp) > .01 and abs(temp) < 99:
            self.t=str(temp)
        else:
            formatStatement='{0:1.'+str(sigFigQues)+'E}'
            self.t=formatStatement.format(temp)


firstRendition=True
print('firstRendition',firstRendition)

x=QuesVar(3, 5, 9, firstRendition)#first,low,high
print(x.t, x.v)

firstRendition=input('firstRendition is ? (True/False)')

firstRendition=False
print('firstRendition',firstRendition)

x=QuesVar(3, 5, 9, firstRendition)#first,low,high
print(x.t, x.v)