Advertentie | |
|
![]() |
|
![]() |
Code:
#!/usr/bin/env python ### # Title: Type Checking # Author: Ben Briggs @ linuxnewbie.org # Date: March 28, 2001 # Written for: CCAE http://www.codeexamples.org # Description: # Illustraits how to check for the types # of objects. ### ### # This line imports all the objects in the # module 'types' without object-oriented # programming. For example, using the import # method, to use the object type 'IntType', # you'd need to type 'types.IntType'. With # the 'from/import' method, you just need # to type 'IntType'. ### from types import IntType ### # Now begins the normal module importation # section. ### import string # atoi() import readline # Enable 'history' of previously entered values (Use up arrow) def main(): ### # Variable declarations. # ---------------------- # The variable named 'num' will initially defined # with the value of '10000', this will kick off # the Number to String/String to Number conversions. ### num = 10000 ### # 'stringspace' will be used throughout the program # for converting numbers to strings, and storing # other strings. # # The builtin function 'str()' converts the argument # given into a string and returns the new string value. ### stringspace = str(num) print "num: %d stringspace: %s" % (num, stringspace) ### # Now perform the number conversion on a literal, # as opposed to a variable. ### stringspace = str(5007) print "num: %d stringspace: %s" % (num, stringspace) ### # This next line uses the 'atoi()' method of the # string module, to convert the string back to a # number. ### num = string.atoi(stringspace) print "num: %d stringspace: %s" % (num, stringspace) ### # The code in this 'try' statement will assign the # string 'good pizza' to the string variable 'stringspace' # if the attempt is insuccessful, Python will print # an error message stating that it cannot make the # conversion. ### try: stringspace = "good pizza" num = string.atoi(stringspace) except ValueError: ### # If the buildin exception 'ValueError' happens, # print this block of code. A 'ValueError' is # storing an inappropriate type in a variable, # like assigning a string to an integer. ### print "Could not convert data to and integer." ### # Print the values again, to see what is stored in # 'num' when the conversion fails. ### print "num: %d stringspace: %s" % (num, stringspace) ### # This 'if' statement checks to see if 'stringspace' # was converted to an integer, if it was... print # a message stating the new value. If the conversion # failed specify that the value in 'stringspace' is # a string, and therefore cannot be converted to a # integer. This is check with the type 'IntType' from # the 'types' module imported at the top of the script. ### if stringspace is IntType: num = string.atoi(stringspace) print "new num: %d" % num else: print "%s is not a number" % stringspace print # A newline is printed ### # The user is prompted to enter a number. ### print "Please enter a number at the prompt. Hit enter on a blank line to exit." ### # This 'while' loop runs while 'stringspace' # is not an empty string (result of pressing # the Enter/Return key without entering text). ### while stringspace != '': ### # A prompt is printed using the builtin # function 'raw_input()', the text passed # as a argument, will be printed at the prompt. # Any text entered at this prompt, will be # stored in the variable 'stringspace'. ### stringspace = raw_input("> ") ### # If any text was entered, Python runs # the code in this 'if' statement. If not... # a "Good Bye" message is printed, and the program # exits. ### if stringspace != '': ### # This 'try' statement attempts to convert # the value(s) entered at the prompt into # an integer. If the conversion is successful, # Python will print a message stating that the # text was a number. If there is a 'ValueError', # Python will print that the text is a string. ### try: num = string.atoi(stringspace) print "%d is a number." % num except ValueError: print "%s is a string" % stringspace else: print "Good Bye!" ### # The next two lines of code check to # see if this script is being loaded # as a module, or directly being executed. # If the answer is the latter, execute the # main() function. ### if __name__ == '__main__': main() |
![]() |
||
Citaat:
het type 'string' bestaat namelijk niet, het is 'String' daarnaast vergeet je 2x een puntkomma en ja, met die fouten weigert ie gewoon daarnaast kan het ook een stuk korter: Code:
int cijfers = 14; String tekens = ""+cijfers; |
![]() |
||
Citaat:
__________________
He can't hear the sirens, cause silence is the greatest sleep of them all.
|
![]() |
|
hehe. anyway mocht je in VB progameeren:
IntegerName = CStr(StringName) de niet officiele manier: Dim IntegerName As Integer Dim StringName As String IntegerName = StringName Maar voortaan issut wel makkelijk als je bijzet welke taal je programmeert, ik heb dit nu waarsgijnlijk voor nix zitten typen ![]()
__________________
$karma++;
|
![]() |
||
![]() |
Citaat:
|
![]() |
||
Citaat:
__________________
He can't hear the sirens, cause silence is the greatest sleep of them all.
|
Advertentie |
|
![]() |
|
|
![]() |
||||
Forum | Topic | Reacties | Laatste bericht | |
Huiswerkvragen: Exacte vakken |
[INF]Help gezocht (Visual Basic) Lothar | 21 | 14-04-2008 21:24 | |
Software & Hardware |
Delphi en text bestanden BendeBen | 2 | 25-01-2007 18:42 | |
Software & Hardware |
getallenstelsels in visual basic.net Land_of_spirits | 25 | 25-09-2006 17:16 | |
Software & Hardware |
[Prog] Delphi: carpool-programma whgeraards | 12 | 21-06-2005 12:39 | |
Software & Hardware |
[Alg] Veilig (web)programmeren Dr HenDre | 9 | 12-04-2005 01:29 |