Difference between revisions of "CSC111 Lab3 Solutions 2014"

From dftwiki3
Jump to: navigation, search
(Solution Programs for Lab 3)
Line 2: Line 2:
 
----
 
----
 
=Solution Programs for [[CSC111 Lab 3 2014| Lab 3]]=
 
=Solution Programs for [[CSC111 Lab 3 2014| Lab 3]]=
<onlydft>
+
 
<br />
 
<br />
 
<source lang="Python">
 
<source lang="Python">
Line 192: Line 192:
 
</source>
 
</source>
 
<br />
 
<br />
</onlydft>
+
 
<br />
 
<br />
 
<br />
 
<br />
 
[[Category:CSC111]][[Category:Python]][[Category:Labs]]
 
[[Category:CSC111]][[Category:Python]][[Category:Labs]]

Revision as of 10:43, 13 February 2014

--D. Thiebaut (talk) 08:55, 12 February 2014 (EST)


Solution Programs for Lab 3


# Solutions  programs for Lab 3
#

# Test case 1
x = int( input( "Please enter an integer: " ) )
if    x > 10     :
    print( "valid input" )
else:
    print( "invalid input" )


# Test case 2
x = int( input( "Please enter an integer: " ) )
if    x > 10 and x < 20    :
    print( "valid input" )
else:
    print( "invalid input" )

# Test case 3
x = int( input( "Please enter an integer: " ) )
if    x >= 10 and x <= 20    :
    print( "valid input" )
else:
    print( "invalid input" )

# Test case 4
x = int( input( "Please enter an integer: " ) )
if  x % 2 == 0:
    print( "valid input" )
else:
    print( "invalid input" )

# Test case 5
x = int( input( "Please enter an integer: " ) )
if    x % 2 == 0 and x % 5 == 0:
    print( "valid input" )
else:
    print( "invalid input" )

# Test case 6
x = int( input( "Please enter an integer: " ) )
if    x % 2 == 0 and x >=0 and x <=100:
    print( "valid input" )
else:
    print( "invalid input" )

#
# LARGEST OF 3
#
x = int( input( "> " ) )
y = int( input( "> " ) )
z = int( input( "> " ) )
if x <= y and x <= z:
    print( x, "is the smallest" )
elif y <= x and y <= z:
    print( y, "is the smallest" )
else:
    print( z, "is the smallest" )

#
# SANTA
#
name = input( "Hello!  What is your name? " )
name = name.lower()

if name.find( "santa" ) != -1:
    print( "Ho, Ho, Ho Santa Claus!" )
else:
    print( "Hello there, " + name + "!" )

#
# MOTHER/FATHER
#
answer = input( "How was your day? " ).lower()
if answer.find( "mother" )!= -1 or answer.find( "father" ) != -1:
    print( "Should we talk about your family?" )
else:
    print( "Glad to hear that!" )
print( "Good bye!" )

#
# ROCK/PAPER/SCISSORS
#
from random import choice

OPTIONS = ['R', 'P', 'S' ]

computer = choice( OPTIONS )
human = input( "What is your play? " ).upper()
 
print( "The compute has chosen", computer )

if ( computer=='R' and human=='P' ) \
   or ( computer=='P' and human=='S' ) \
   or ( computer=='S' and human=='R ):
    print( "Congrats, you win!" )
        
elif ( computer=='R' and human=='S' ) \
   or ( computer=='P' and human=='R' ) \
   or ( computer=='S' and human=='P ):
    print( "Sorry, you lose this time!" )

else:
    print( "It's a tie!" )


#
# keeping scores
#

computerScore = 0
humanScore = 0

# Round 1        
computer = choice( OPTIONS )
human = input( "What is your play? " ).upper()

        
print( "The compute has chosen", computer )

if ( computer=='R' and human=='P' ) \
   or ( computer=='P' and human=='S' ) \
   or ( computer=='S' and human=='R ):
    print( "Congrats, you win!" )
    humanScore = humanScore + 1
        
elif ( computer=='R' and human=='S' ) \
   or ( computer=='P' and human=='R' ) \
   or ( computer=='S' and human=='P ):
    print( "Sorry, you lose this time!" )
    computerScore = computerScore + 1
else:
    print( "It's a tie!" )
        
# Round 2        
computer = choice( OPTIONS )
human = input( "What is your play? " ).upper()

        
print( "The compute has chosen", computer )

if ( computer=='R' and human=='P' ) \
   or ( computer=='P' and human=='S' ) \
   or ( computer=='S' and human=='R ):
    print( "Congrats, you win!" )
    humanScore = humanScore + 1
        
elif ( computer=='R' and human=='S' ) \
   or ( computer=='P' and human=='R' ) \
   or ( computer=='S' and human=='P ):
    print( "Sorry, you lose this time!" )
    computerScore = computerScore + 1
else:
    print( "It's a tie!" )

# Round 3        
computer = choice( OPTIONS )
human = input( "What is your play? " ).upper()

        
print( "The compute has chosen", computer )

if ( computer=='R' and human=='P' ) \
   or ( computer=='P' and human=='S' ) \
   or ( computer=='S' and human=='R ):
    print( "Congrats, you win!" )
    humanScore = humanScore + 1
        
elif ( computer=='R' and human=='S' ) \
   or ( computer=='P' and human=='R' ) \
   or ( computer=='S' and human=='P ):
    print( "Sorry, you lose this time!" )
    computerScore = computerScore + 1
else:
    print( "It's a tie!" )

if  computerScore > humanScore:
    print( "You lose the round of 3 games! :-(" )
elif computerScore < humanScore:
    print( "You win the 3 rounds!  Congrats!" )
else:
    print( "Your 3 rounds end in a tie!" )