Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9th 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. 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? 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!') hello variable hello = 'Hello world! ' print(hello) We use hello as a variable User input their name print('What is your name?') myName = input() Use the input() function 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 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 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!') 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 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. 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') 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? 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 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 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')