ࡱ> DFC` ejbjb $(|c|c,8rrrrrrr$$$8\ h$3555555,R aralrrvlllrr3l4"rrrr3llrr3 I)$<030, !l !3lr D  I/O: Input-Output I/O is short for input-output. Input usually means the data entered by the end-user of the program. When you use Microsoft Word, you are playing the role of the end-user (sometimes shortened to just plain 'user'). The player of a video game is its 'end-user'. When programming, we often switch between playing the role of programmer and end-user. We're the end-user when we are testing whether the program works. We usually call the program itself the system, as in 'when the end-user enters a number, the system responds by...' Input can also come from a file, e.g., when you type 'python someprog.py' the program called 'python' is getting its input from a file called someprog.py. Output is what the program produces. For command-line programs, its just whatever is printed on the screen. Of course for programs with graphical user interfaces, its much more complex. User Input in Python Python has two key functions to deal with end-user input, one called raw_input() and one called input(). When you call these functions, the system will wait for the end-user to enter something. The system returns the user's entry as a string result from the function. If you execute the statement: >> result = raw_input() Python will wait for you (the end-user) to enter something. Enter some text and hit return. Then type >> print result This should print whatever the end-user typed. Generally, we want to prompt the user for input. You can do this by adding a parameter to the 'raw_input()' call. Open a text editor and add create a file with the following lines: x = raw_input('please enter a string:') print x will look like: please enter a string: hello hello User input and types The function raw-input() always returns a string. So if the user enters 7, Python interprets it as the string '7'. Sometimes this is fine, but often we want to perform computations on numbers, and as far as the computer is concerned, '7' is a symbol and not a number. If you want to 'interpret' input as an integer, you need to convert it. Python provides some type conversion functions to do so, such as: x=int('7') # puts the integer 7 into x. f=float('7.7') #puts the floating point number 7.7 into f. Using a type converter, one could get an integer from the user, and perform a computation on it, with the following: s=raw_input('enter a number:') x = int(s) y = x+3 print y This will print out 3 more than whatever the user enters. Because this converting is a bit laborious, Python provides the 'input()' function, which converts things automatically. 'input()' basically looks at what the user enters, and automatically determines the correct type. x = input('enter a number:') y = x+3 The only problem with 'input' is you have to assume the user will enter what you are expecting. If they don't, you are in trouble. For instance, if a user entered xyz for the input statement above, the program would bomb. For now, just use the input() function when you are expecting an integer from the end-user, and raw_input when you are expecting a string. Later, will talk about validating user input in detail. In-Class Assignment: Mastermind beginnings: Write a python program that prompts the user for four colors, gets the input, and builds a list named guess which has four elements of type string. Heres a sample of how your program should behave (with user input in bold) Please enter a color: red Please enter a color: blue Please enter a color: green Please enter a color: red Your guess is: [red, blue, green, red] CS 110: Introduction to Computer Science Spring 2008 _g +eᶯh hh!h!hh!hB*CJOJQJph h_sh$hh5B*CJOJQJph!h_shB*CJOJQJph hh hp|h23yzst*+ST9 : b c gd^gdgd$a$gd,dc L M b c H I r { | H I gdgd^gd`gd mnQRqgd`gd,bcdegd & Fgd ":p/ =!"#$%<@< NormalCJaJmH sH tH T@T #t Heading 1$<@&5CJ KH OJQJaJ R@R #t Heading 2$<@&56CJOJQJaJDA@D Default Paragraph FontRi@R  Table Normal4 l4a (k@(No List4@4 #tHeader  !4 @4 #tFooter  !0U@0  Hyperlink>*B*e ( z z zeu23yzst*+ST9:bcLMbcHIr{|HImnQ R q ,bf0@0ʀ000ʀ0ʀ0ʀ000ʀ0ʀ00ʀ0ʀ0ʀ00ʀ0ʀ00ʀ0ʀ0ʀ0ʀ000ʀ0ʀ0000ʀ0000ʀ0000ʀ00ʀ0ʀ0ʀ0ʀ0ʀ0ʀ0Љ`0ʀ000000000000000ʀ0ʀ0ʀ0ʀ0ʀ0ʀ00ʀ00ʀ0ʀ0ʀ0ʀ0пPv0п̀0п0 ʀ 0 ʀ0ʀ0ʀ0ʀ0ʀ0ʀ0ʀ0ɀ 0d 023yzst*+ST9:bcLMbcHIr{|HImnQ R q f@ ;>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p>9p077777:ec edejuNWtwT ] z } l t & / ,fTX :UZJKsx|rsR S v w  " U Z ,f::::::::::::::::::::::::w:n€hh^h`o(. 88^8`hH. L^`LhH.   ^ `hH.   ^ `hH. xLx^x`LhH. HH^H`hH. ^`hH. L^`LhH.w:n         @`2e @UnknownGTimes New Roman5Symbol3 ArialC Lucida Grande qh ¦ ¦ !24d+ |@XPMacintosh HD:Applications:Microsoft Office 2004:Templates:My Templates:cs110.dotLinux TutorialUSF ITSUSF ITS  Oh+'0(  , 8 D P\dlt|'Linux TutorialiUSF ITSSF  cs110.dotriUSF ITS3F Microsoft Word 11.1@`4<@@S@ e@6F e G8'PICT'.d ,, MSWD ,Times New Roman 2.(w CS 110: I- Q)n-\)t-) r-)o-)duc-)Ht-`)i-)o- Q)n -)&t-\)o- Q) -) C-)!o-`)m-)'pu-)2t-)e-)r-) Sc-`)?i-)e- Q)n-)ce!-  ), -). (Spr-`)Fi- Q)n-)g-) -) 2008-\)d ,Arial B-(I->)/-)O->)3:-) ->)In-)<pu->)Pt-)-->)O-)4u->)(tp-)@ut- )>  2-(w  6-*@I/O is short for input(%-)output.-) (dw -*@Input u-)s-)ua-)@l-)ly- K), -)means-) -)the- K)T -)data-)r -)entered-) -)by-h)> -)the-)T -)end- K)g--)#user){ of the- K) -(w program. -8(z -($w -+@#When you use Microsoft Word, you ar- K(de-) playing-) -)the- K)T -)role-)e -)of- K)5 -)the-)T -( end)b-)user (sometimes shortened to- K( -)just-)b -)plain- K) -)'user'- K))-).-) -)The- K)b -( player of a vide(o game is its 'end(n-)user- K)r'-) .-) (#w -*@>When programming, we often switch between playing the role of *@program- K)m-)3er-)4 -)and- K)b -)end)b-)user. -8) -(w -+?We're- K) -)the-)T -)end)b-),user when we are testing whether the program- K("_ -(b works. We usua-(bl-)ly- K), -)call-)Z -)the- K)T -)program-) -)it-)$s-)elf- K)B -)the-)T -)system,- K) -)as-): -)in- K)2 -( 'when the end(}-)user en)ters a number, - K(t-)he-)@ -)system- K) -)responds-) -( by...'-8)} -("w -*? Input can -(aa-)lso- K)M -)come-) -)from- K)} -)a-) -)file,- K)c -)e.g.,-)s -8) -(w -+@when you type 'python s- K(o-)"meprog.p- K)y-)'-)  - K)t-)he-)@ -)program- K) -)called-) -(! ='python' is getting its input from a file called someprog.py.-8(! -(aw -*?&Output is what the program produces. -( (w -*@For co- K)m-)3mand)-)lin)B0e programs, its just whatever is printed on the (`wscreen. -) (w -*?%Of course for programs with graphical-(e -)user- K)r -) interfaces,-(  -)its- K)@ -)mu-)Tc-)h ( wmore c- K)o-)"mplex.-8) -( _w ,Arial :-*tUser -F)I-)n-F)#p-)$u-F)#t-) i-F) n-)$ Py-F)Wth-)8on-)F  6-( w -*@Python has two key functio-( `<n-)!s- K) -)to-)5 -)deal- K)n -)with-)p -)end)b-)user in-)p-)!ut,- K)G -)one-)a -( w called raw_-( i-)nput(- K))-) -)a-)nd- K)D -)one-)a -)ca-):l-)led- K)P -)i)npu-)ft-)(- K))-).-) -)When- K) -)you-)_ -)ca-):l-)l- K) -)these-) -( wfunctio-)n-)!s,- K)- -)the-)T -)system- K) -)will-)Z -)w-)*a-)it- K)$ -)for-)K -)the- K)T -)end)b-)user to enter so- K( m-)3ething.-) -) ( w -*@&The system returns the user's entry as- K( `c -)a-) -)string- K) -)res-)Pu-)!lt- K)$ -)from-)} -)the-)T -( w function.-`)  ! ! ! !  ! ! !  ! ! !  ! ! !  ! ! !  ! ! !  ! ! !  ! ! !  ! ! ! ՜.+,0h `hpx  0'+  Linux TutorialI/O: Input-Output User Input in Python User input and types In-Class Assignment: Title Headings  !"#$%'()*+,-./0123456789:<=>?@ABERoot Entry FjdG1Table!WordDocument$(SummaryInformation(&(DocumentSummaryInformation8;CompObjX FMicrosoft Word DocumentNB6WWord.Document.8