CSCE 110 — Programming IBasics of Python: Decision-Making and RepetitionDr. Tiffani L. WilliamsDepartment of Computer Science and EngineeringTexas A&M UniversityFall 2011if StatementIThe standard if conditional statement follows this syntax:if expression:if_suiteIIf the expression is non-zero or True, the if_suite isexecuted. Then, execution continues on th first statementafter the if block.ISuite is the term used in Python to refer to a sub-block ofcode and can consist of single or multiple statements.IParentheses are not required in if statements as they are inother languages.if/else Statementif expression:if_suiteelse:else_suiteodd-even.pyListing 1: odd-even.py# D et erm in e w he th er the u ser en te r ed an ev en or o dd n um be r .us er_ in put = r aw _in pu t ( ’ Please e nt er a num be r : ’) #1nu mb er = int ( u ser _i npu t ) #2if ( n um be r % 2 == 0): #3print number , ’is an even numbe r . ’ #4else : #5print number , ’is an odd n um be r . ’ #6 if-elif-else StatementFor the if/elif/else statement, you can have an unlimited number ofelif expressions.if expression:if_suiteelif expression:elif_suiteelse:else_suiteDetermining letter grade from a scoreListing 2: if-elif-else.py# An ex am pl e of how to use t he if - elif - e lse co n st ru c t .score = int ( r aw_ in put (" Please en te r your scor e : " )) #1if score >= 90: #2le tt er = ’A ’ #3print " Yo u ob vio us ly st udied !"elif sc or e >= 80: #4le tt er = ’B ’ #5elif sc or e >= 70: #6le tt er = ’C ’ #7elif sc or e >= 60: #8le tt er = ’D ’ #9else : # 10le tt er = ’F ’ # 11print " S uc cess ?? ?! ! "print " Here ’ s yo ur l et te r grad e : " , l et te r # 12 Determining letter grade from a score (bad example withuse of if-else)Listing 3: if-else-bad-example.py# Usin g if - e lse co n st ru c ts to d is pl ay a l et te r g rad e .# In t hi s cas e , if - e lif - el se c on s tr uc t w ou ld be be tt er .score = int ( r aw_ in put (" Please en te r your scor e : " )) #1if score >= 90: #2le tt er = ’A ’ #3else : #4if score >= 80: #5le tt er = ’B ’ #6else : #7if score >= 70: #8le tt er = ’C ’ #9else : # 10if s co re >= 60: # 11le tt er = ’D ’ # 12else : # 13le tt er = ’F ’ # 14print " Here ’ s yo ur l et te r grad e : " , l et te r # 15 while Loopwhile expression:while_suiteCounting with while loopsListing 4: counting-with-while-loop.pycount = 5 #1while count == 5: # 2print " th e value of count is " , count #3# c ou nt += 1 #4print " Done w ith w hi le l oop !! " # 5 guessing-game.pyListing 5: guessing-game.py# A gam e wh er e the u ser gu es s es a secre t nu mb er be tw een 1 and 1 00.im po rt ra nd om #1so lu tio n = r an do m . r an di nt (1 , 100) # 2guess = 0 #3at te mpt s = 0 #4while guess != s ol ution : #5at te mpt s = at te mpt s + 1 #6us er_ in put = r aw _in pu t ( ’ En te r a n um be r b et we en 1 an d 1 00: ’) #7guess = int ( u ser _i npu t ) # 8if guess == s ol ution : #9print ’ C o ng r at u la t io n s ! It took you % d g ue ss es . ’ % ( at te mpt s ) # 10elif gu es s < so lu tio n : # 11print ’ H IG HE R ’ # 12else : # 13print ’ L OWER ’ #14 guessing-game-computer.pyListing 6: guessing-game-computer2.py# The c om p ut er at t em pt s to gu es s a n um be r s el ec te d by the u ser .im po rt ra nd om #1re sp ons e = ’ w ha tever ’ #2at te mpt s = 0 #3min = 1 #4max = 100 #5print ’ My name is Al ice . ’ #6print ’I will try to gu es s a s ec ret n um be r betwe en 1 and 1 00. ’ #7while re spo ns e != ’c ’: # 8at te mpt s = at te mpt s + 1 #9guess = (( max - min ) / 2) + min # 10print " A lice ’s gu es s : " , gu ess # 11re sp ons e = ra w_i np ut ( ’ (c) orrect (h) ig he r ( l) ower : ’) # 12if re spons e == ’h ’: # 13print ’ H IG HE R ’ #14min = gu es s + 1 #15else : #16print ’ L OWER ’ #17max = gu es s - 1 #18print ’ C OR RECT ! It took % d a ttemp ts . ’ %( att em pts ) # 19
View Full Document