Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 7 Online
OpenStudy (anonymous):

this is with regards to the hashing and classes lecture. The lecture code shows def readVal(valType, requestMsg, errorMsg): numTries = 0 while numTries < 4: val = raw_input(requestMsg) try: val = valType(val) return val except ValueError: print(errorMsg) numTries += 1 raise TypeError('Num tries exceeded') _-------------------------------------- why does val = valType(val) results in an exception ValueError if my valType is int. and my val is a float example 2.9 isnt val = suppose to be an assignment of an object to a value?

OpenStudy (anonymous):

First of all I want to spread mccornacks word: In order to make it possible to have spaces preserved when copying you can enter ``` on a new line and it is the ` on the ~-button (followed by an Enter) to get a block started where you can copy your code into def readVal(valType, requestMsg, errorMsg): numTries = 0 while numTries < 4: val = raw_input(requestMsg) try: val = valType(val) return val except ValueError: print(errorMsg) numTries += 1 raise TypeError('Num tries exceeded') # MAIN print readVal(int, 'Enter int: ', 'Not an int.') ``` That way anybody can copy and paste the code without having to edit line ends and idents etc. back in

OpenStudy (anonymous):

I'm still working on ps4 so a few more lectures to go. However I think that anything you enter is in assigned to val as a string. the print readVal(int, ..., ...) assigns int to valType and in the try-part the value of val is assigned if it can be converted safely to the required type a float cannot ingeneral be assigned to an integer without being changed if you would execute print readVal(float, ..., ...) any integer value is turned to its floating point equivalent I can't find the formal description valType() on internet either

OpenStudy (anonymous):

Oh, and to start the block with ``` on a new line That dissapeared in my first message over here

OpenStudy (anonymous):

valType is what the user enters into the function . e.g readVal(int(valtype), "enter an int","not an int")

OpenStudy (anonymous):

Well, I might be able to give a better anwer once I've don lecture 10, but I'm still struggeling to do problem 4b, so that will not be versy soon I'm afraid. Hopefully somebody else will come to the rescue

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!