Download CS1315: Introduction to Media Computation

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Nested Conditional
if (condition):
statements
else:
statements
if a > b:
c=0
else:
c=20
if (condition):
if a > b:
if x == y:
c=0
else:
c=-4
else:
c=20
if (condition):
statements
else:
statements
else:
statements
Nested Conditional: elif
if (cond1):
…
else:
if (cond2):
…
else:
if (cond3):
…
if (cond1):
…
elif (cond2):
…
elif (cond3):
…
if grade>=90:
letGrade =‘A’
else:
if grade >=80:
letGrade=‘B’
else:
if grade>=70:
if grade>=90:
letGrade =‘A’
elif grade>80:
letGrade=‘B’
elif grade>=70:
letGrade=‘C’
else:
letGrade = ‘LOW”
Eliza

A word game to simulate Gestalt psychotherapist




Hello. Welcome to therapy. What is your name ? Tim
Well Tim, What can we do for you today? I am writing
a book on Python
Tell me more. Do you know Python?
Why do you want to know?
Eliza
Lab_0229
 Modify Eliza program
 Add the main (control) program that repeated ask
for your response and call function runEliza()
 Add at least one ‘elif’ case
 Add more cases to handle
 Email to [email protected]
Guessing game

Computer picks a random integer between 0 and 19

You have three chances to guess the number
Guessing game

Python code
 to generate a random integer in (0,20) (use
variable ‘number’ to save it)?

to enter a number to guess via keyboard (use
‘guess’) ?

to match ‘number’ and ‘guess’ up to 3 times (use
‘guessTaken’ to count) ?
 ‘for’ loop or ‘while’ ?
Number Guess
import random
guessesTaken = 0
number =
while guessesTaken < 3:
guess =
guessesTaken =
if
print('Your guess is too low.’)
if
print('Your guess is too high.’)
if
break
if guess == number:
print (‘You win!’)
else:
print (‘You lose.’)
Number Guess
import random
import random
guessesTaken = 0
guessesTaken = 0
number = random.randint(1, 20)
while guessesTaken < 3:
guess = random.randint (1,20)
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.’)
if guess > number:
print('Your guess is too high.’)
if guess == number:
break
if guess == number:
print (‘You win!’)
number = random.randint(1, 20)
while guessesTaken < 3:
guess = random.randint (1,20)
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.’)
elif guess > number:
print('Your guess is too high.’)
else:
break
if guess == number:
print (‘You win!’)
else:
else:
print (‘You lose.’)
print (‘You lose.’)