1 / 16

Hands on Projects

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.

alaula
Download Presentation

Hands on Projects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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.

  3. 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?

  4. Game1: Greeting hello = '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!')

  5. hello variable hello = 'Hello world! ' print(hello) • We use hello as a variable

  6. User input their name print('What is your name?') myName = input() • Use the input() function

  7. 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

  8. 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

  9. 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!')

  10. 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

  11. 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.

  12. Guess a number import random #import the random library number = 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')

  13. 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?

  14. Q: How does a computer generate a random number? import random #import the random library number = 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

  15. 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

  16. 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')

More Related