16
Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

Embed Size (px)

Citation preview

Page 1: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

Hands on Projects

Dr. Bernard Chen Ph.D.University of Central Arkansas

July 9th 2012 CS4HS@UCA

Page 2: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

Game 1: Greetings The computer asks a player to input his/her

name. Afterwards, the computer greetings the

player with the name. The computer offers the player an

opportunity to view the encoding of the name.

The computer displays the encoding if the player chooses to do so. Otherwise, the computer says good-bye.

Page 3: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

Questions for this game Q: How does a computer ask a

player to type in his/her name? Q: How can a computer remember

the player’s name? Q: How does a computer offers the

player the opportunity to view the encoding of the name?

Q: How does computer change name into numbers?

Page 4: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

Game1: Greetinghello = 'Hello world! 'print(hello)print('What is your name?')myName = input()print('\nIt is good to meet you, ' + myName)print('I store your name as integers. \n')res = input('Do you want to see them (Y/N)?')res = res.lower()if res == 'y' or res == 'yes': print('Yes, here is how I stored your name:') for x in myName: print(x, ' => ', ord(x))else: print('All right, see you next time') print('Bye!')

Page 5: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

hello variable

hello = 'Hello world! 'print(hello)

We use hello as a variable

Page 6: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

User input their name

print('What is your name?')myName = input()

Use the input() function

Page 7: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

Print out user’s name

print('\nIt is good to meet you, ' + myName)print('I store your name as integers. \n')

\n is used for print a new line print() function is actually very powerful, it can

print not only “strings” but also variables You can use “,” or “+” to connect two things

(or more) together with space to separate them

Page 8: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

Give user a choice,change res to lower case

res = input('Do you want to see them (Y/N)?')res = res.lower()

Another type of input() function We can actually put the information display

for user inside of the input() function Change user’s input into lower case by using

res.lower() function

Page 9: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

If statement

if res == 'y' or res == 'yes': print('Yes, here is how I stored your

name:') for x in myName: print(x, ' => ', ord(x))else: print('All right, see you next time') print('Bye!')

Page 10: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

The chr( ) and ord( ) functions The chr(n) function

takes an integer ASCII value n and returns a string with that ASCII value's character. >>> chr(65)'A’>>> chr(66)'B‘>>> chr(53)'5’

The ord(c) function takes a single character c and returns the integer ASCII value of c.>>> ord('A')65>>> ord('B')66>>> ord('5')53

Page 11: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

Game 2: Guess a number The computer generates a random number. Afterwards, the computer asks user to input

one number. The computer checks the number to

determine if the user guesses the correct number, if not, the computer provides the hint.

The computer only let the user guess 3 times at most.

Page 12: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

Guess a numberimport random #import the random librarynumber = random.randint(1,100)for x in range(3): print('Guess a number between 1 and 100:') myGuess = int(input()) if (myGuess == number): print ('You win! Congratulations! ') break elif (myGuess < number): print ('Go up') else: print ('Go down')

Page 13: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

Questions for this game Q: How does a computer generate a

random number? Q: How can a computer tell that the user

guessed the correct number? Q: How does a computer provides the

hint to user if the guessed number is incorrect?

Q: How does a computer control the number of times that a user can guess?

Page 14: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

Q: How does a computer generate a random number?

import random #import the random librarynumber = random.randint(1,100)

How do we generate a random number between one and one hundred? We have a function named randint() available

in the module named random We import the module, then call the function

Page 15: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

User input a number

for x in range(3): print('Guess a number between 1 and

100:') myGuess = int(input())

Use for loop here to control how many times that a user can guess

Page 16: Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012 CS4HS@UCA

Q: How does a computer provides the hint to user if the guessed number is incorrect?

if (myGuess == number): print ('You win! Congratulations! ') break elif (myGuess < number): print ('Go up') else: print ('Go down')