Six Programming Tasks

28
8/10/2019 Six Programming Tasks http://slidepdf.com/reader/full/six-programming-tasks 1/28  Rajput [SIX PROGRAMMING ACTIVITES] Pseudo code in green, actual code in yellow  Task one: Generating a Username  The objective of this task is for a user to be able to input their rst, last names and date of birth to generate a custom username and password! This script should be able to generate the user"s rst name, their surname and their date of birth! The username generated should be the rst two characters from their rst name, their surname and then the year section of their date of birth e!g! #ohn $mith born in %&'( would have the username #o$mith'(! import random import string print)*Username and Password Generator*+ rstname input)*-nput your rst name: *+ surname input)*-nput your surname: *+ dob input)*-nput your date of birth e!g).%/.%/%&&0+: *+ username dob123:4 5 surname 5 rstname1.4 def randompassword)+:  charsstring!ascii6uppercase 5 string!ascii6lowercase 5 string!digits  si7e0 return 88!join)random!choice)chars+ for 9 in range)si7e,%3++ print)*our Username is: * 5 username+ print)*our Password is: * 5 randompassword)++

Transcript of Six Programming Tasks

Page 1: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 1/28

 Rajput [SIX PROGRAMMING ACTIVITES]

Pseudo code in green, actual code in yellow

 Task one: Generating a Username

 The objective of this task is for a user to be able to input their rst, last names

and date of birth to generate a custom username and password! This scriptshould be able to generate the user"s rst name, their surname and their date of

birth! The username generated should be the rst two characters from their rst

name, their surname and then the year section of their date of birth e!g! #ohn

$mith born in %&'( would have the username #o$mith'(!

import random

import string

print)*Username and Password Generator*+

rstname input)*-nput your rst name: *+

surname input)*-nput your surname: *+

dob input)*-nput your date of birth e!g).%/.%/%&&0+: *+

username dob123:4 5 surname 5 rstname1.4

def randompassword)+:

  charsstring!ascii6uppercase 5 string!ascii6lowercase 5 string!digits

  si7e0

return 88!join)random!choice)chars+ for 9 in range)si7e,%3++

print)*our Username is: * 5 username+

print)*our Password is: * 5 randompassword)++

Page 2: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 2/28

 Rajput [SIX PROGRAMMING ACTIVITES]

Page 3: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 3/28

 Rajput [SIX PROGRAMMING ACTIVITES]

import random module

import string module

display; Username and Password Generator*

dene rstname as input)*-nput your rst name: *+

dene surname as input)*-nput your surname: *+

dene dob as input)*-nput your date of birth e!g).%/.%/%&&0+: *+

dene username as dob123:4 5 surname 5 rstname1.4

deng new variable as randompassword)+:

  charsupper case characters 5 lower case characters 5 digits

  si7e0

return 88!join)random!choice)chars+ for 9 in range)si7e,%3++

display *our Username is: * 5 username

display *our Password is: * 5 randompassword)+

Page 4: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 4/28

 Rajput [SIX PROGRAMMING ACTIVITES]

-ssues during development solution

• <orgot to put closing and

opening parentheses for print

statements• =id not know how to select

certain part of te9t )e!g rst two

letters of >hello;+• ?isspelled certain function

• - added the missing parentheses

after - received a synta9 error!• - had to research how to do this

on the web• @orrected function spelling as it

turned up as an undenedvariable or synta9 error!

Evaluation

Averall - found this task a great challenge as - am fairly new to python and felt

that it went fairly well with minimal errors due to mistakes and not knowing some

features of python that are now a part of my python vocabulary in the future!

Page 5: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 5/28

 Rajput [SIX PROGRAMMING ACTIVITES]

 Task 3: Guess the number

@reate a game that generates a random number between % and %.!

Prompt the user of the program/game to guess what the number is by entering a

number between % and 3. and provide them with feedback if they are too high,or too low!

-f they get the number correct rst time a message is printed which says >BowC

 ou"ve guessed the number at the rst attemptC;! -f the user guesses correctly

after any more guesses they get a message that says >well done, you have

guessed the number;

import random

numerGuesses .

print)*DelloC Bhat is your nameE*+

myFame input)+

number random!randint)%,%.+

print)*ok*, myFame, *,- am thinking of a number between % and %.!*+

while numerGuesses %.:

  print)8Take a guess!8+

guess input)+

  guess int)guess+

  numerGuesses numerGuesses 5 %

  if guess number:

  print)8our guess is too low!8+

if guess H number:

Page 6: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 6/28

 Rajput [SIX PROGRAMMING ACTIVITES]

  print)8our guess is too high!8+

  if guess number:

  break

if guess number:

  numerGuesses str)numerGuesses+

  print)8Good job, 8 5 myFame 5 8C ou guessed my number in 8 5

numerGuesses 5 8 guessesC8+

if guess C number:

  number str)number+

  print)8Fope! The number - was thinking of was 8 5 number+

Page 7: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 7/28

 Rajput [SIX PROGRAMMING ACTIVITES]

Page 8: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 8/28

 Rajput [SIX PROGRAMMING ACTIVITES]

$et variable numberGuesses to eIual .

=isplay helloC Bhat is your nameE

$et variable myFame as input)+

$et variable number to a random integer value between % and %.

=isplay ok myFame, - am thinking of a number between % and %.

Bhile numberGuesses %.

=isplay take a guess

Fet variable guess is set as input)+

$et new variable as integer

numberGuesses no eIual to numberGuesses 5 %

 if guess number:

  display 8our guess is too low!8

  if guess H number:

  display 8our guess is too high!8

  if guess number:

if guess number:

  numerGuesses string)numerGuesses+

  display 8Good job, 8 5 myFame 5 8C ou guessed my number in 8 5 numerGuesses5 8 guessesC8

if guess C number:

  number string)number+

  display 8Fope! The number - was thinking of was 8 5 number

Page 9: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 9/28

 Rajput [SIX PROGRAMMING ACTIVITES]

Page 10: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 10/28

 Rajput [SIX PROGRAMMING ACTIVITES]

-ssues during development solution

• =id not add the >C; in > if guess C

number:;

• - added the C after researching

the issue

Evaluation

Averall - thought that this task challenging however was fairly simple after -

started coding! - did some more research to aid my skills in python! This task was

a success and the script has been coded eJciently and has not used any

unnecessary functions!

 Task 3: quiz ti!

Kasic: our program must print L Iuestions like the ones below and allow the

user to enter an answer!

Mdvanced: ou program must allow user % to dene how many Iuestions they

want to be in their Iui7, and then allow them to set the country and capital cities

for use in each Iuestion! User 3 will then be prompted for their name and to

complete the Iui7 using the Iuestions set by user %!

print)*Dello and welcome to the capital city Iui7C*+

name input)*Bhat8s your nameE *+

answer **

totalPoints .

print)*NnDello * 5 name 5 *!our will now be asked L IuIestions about capital cities,be sure to remember capital lettersC!*+

print)*Nnou will get O score on the rst Iuestion if answered correctly and will

increase as the diJculty of the Iuestions get harder!*+

score O

while score H .:

  answer input)*uestion %: Bhat is the capital of The United QingdomE: *+

  if answer *ondon* or answer *london*:

  print)*@orrectC * 5 *ou gained * 5 str)score+ 5 * score!*+

  totalPoints 5 score

  break

Page 11: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 11/28

 Rajput [SIX PROGRAMMING ACTIVITES]

  else:

  score 2 %

score (

while score H .:

  answer input)*uestion %: Bhat is the capital of <ranceE: *+

  if answer *Paris* or answer *paris*:

  print)*@orrectC * 5 *ou gained * 5 str)score+ 5 * score!*+

  totalPoints 5 score

  break

  else:

  score 2 %

score %3

while score H .:

  answer input)*uestion %: Bhat is the capital of The United states of MmericaE:*+

  if answer *Bashinton =!@* or answer *washington d!c* or answer *Bashington* or answer *washington*:

  print)*@orrectC * 5 *ou gained * 5 str)score+ 5 * score!*+

  totalPoints 5 score

  break

  else:

  score 2 %

score 3S

while score H .:

  answer input)*uestion %: Bhat is the capital of -ndiaE: *+

  if answer *Few =elhi* or answer *new delhi*:

  print)*@orrectC * 5 *ou gained * 5 str)score+ 5 * score!*+

  totalPoints 5 score

  break

  else:

  score 2 %

score S0

Page 12: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 12/28

 Rajput [SIX PROGRAMMING ACTIVITES]

while score H .:

  answer input)*uestion %: Bhat is the capital of ThailandE: *+

  if answer *Kangkok* or answer *bangkok*:

  print)*@orrectC * 5 *ou gained * 5 str)score+ 5 * score!*+

  totalPoints 5 score

  break

  else:

  score 2 %

score &(

while score H .:

  answer input)*uestion %: Bhat is the capital of $wedenE: *+

  if answer *$tockholm* or answer *stockholm*:

  print)*@orrectC * 5 *ou gained * 5 str)score+ 5 * score!*+

  totalPoints 5 score

  break

  else:

  score 2 %

print)*Nn* 5 name 5 * your score is: * 5 str)totalPoints++

if totalPoints %0&:

  print)*@ongratulations you have successfully completed the Iui7 without errorC*+

elif totalPoints H &L:

  print)*Bell done you have completed the Iui7 with a score above L.*+

else:

  totalPoints &L  print)*ou have completed the test but have scored below L.*+

Page 13: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 13/28

 Rajput [SIX PROGRAMMING ACTIVITES]

Page 14: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 14/28

 Rajput [SIX PROGRAMMING ACTIVITES]

-ssues during development solution

•  There were a few synta9 errors

where - had forgotten to put

colons and brackets, but apartfrom that no major errors

• ?inor errors were corrected and

9ed

Page 15: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 15/28

 Rajput [SIX PROGRAMMING ACTIVITES]

Evaluation

- feel that this was a success and the task was completed smoothly and Iuickly!<urthermore there was little error and found this task interesting and interactive!

 Task S: Test $cores

M teacher wants a program that allows her to enter test scores for her class and toanalyse the results! -n future she may want to be able to enter marks for more thanone test but this is just a prototype!

 The program must allow the teacher enter the number of students in her class, and

enter the names of each student! Ance this is set up, she should be able to enter theresults for one test and nd out the percentage scores and a mean average for theclass! Mfter this she should be asked if she would like to save the data to a te9t le,entering for yes, and F for no!

def -ntalue6get)+:

  num 2%

  while num .:

  try:

  num int)input)++

  e9cept:

  print)*?ust be an integer value*+

  return num

def <ile$avePrt)n+:

  te9t6le open)*test$cores!t9t*,*w*+

  te9t6le!write)*Test $cores*+

  for i in range)., n+:

  te9t6le!write)str)Fames1i4+ 5 *, * 5 str)class$cores1i4++

  te9t6le!close)+

def getPercentages)n+:

Page 16: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 16/28

 Rajput [SIX PROGRAMMING ACTIVITES]

  print)*The percentage marks are: *+

  for i in range)., n+:

  percentage round))class$cores1i4/testTotal+V%..,3+

  print)Fames1i4 5 * * 5 str)percentage+ 5 **+

def meanPercentages)+:

  print)*The mean percentage is: *+

  mean6% sum)class$cores+

  mean63 mean6% / student@ount

  print)mean63+

student@ount input)*Bhat is the total amount of candidatesE: *+

student@ount int)student@ount+

class$cores 1Fone4 V student@ount

Fames 1Fone4 V student@ount

for i in range)., student@ount+:

  Fames1i4 input)*$tudent name: *+

  print)*$tudent score: *+

  class$cores1i4 -ntalue6get)+

print)*Bhat was the test out ofE: *+

testTotal -ntalue6get)+

getPercentages)student@ount+

meanPercentages)+

stop <alse

while stop <alse:

  print)*Nn%! $ave to Te9t leNn3! W9itNn*+

  print)*option: *+

  option -ntalue6get)+

Page 17: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 17/28

 Rajput [SIX PROGRAMMING ACTIVITES]

  if option %:

  <ile$avePrt)student@ount+

  elif option 3:

  stop True

  else:

  option input)*Wntry is -nvalidC*+

Page 18: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 18/28

 Rajput [SIX PROGRAMMING ACTIVITES]

Page 19: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 19/28

 Rajput [SIX PROGRAMMING ACTIVITES]

Page 20: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 20/28

 Rajput [SIX PROGRAMMING ACTIVITES]

-ssues during development solution

•  There were a few synta9 errors

where - had forgotten to put

colons and brackets, but apartfrom that no major errors

• ?inor errors were corrected and

9ed

Evaluation

Averall this task was fairly diJcult as it involved dening many processes in

order to compress the code, reducing that amount code is repeated

unnecessarily!

 Task L: ?easurement @onverter

 M builder wants a prototype of a mobile app which converts imperial measurementsto metric measurements and vice versa! De wants to be able to select from a menuone of the following options:

%! -nches to @entimetres

3! @entimetres to -nches

O! <eet to ?etres

S! ?etres to <eet

def convert)choice,value+:

  if choice %:

  result round)value V 3!LS,3+

  print)str)result+ 5 * cm*+

  elif choice 3:

  result round)value V .!O&O'..'0',3+

  print)str)result+ 5 * -nches*+

  elif choice O:

  result round)vlaue V O!30.0O&&,3+

  print)str)result+ 5 * ?etres*+

  elif choice S:

  result round)value V .!O.S0,3+

  print)str)result+ 5* <eet*+

Page 21: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 21/28

 Rajput [SIX PROGRAMMING ACTIVITES]

def print?enu)+:

  print)*Nn%! -nches to @entimetres*+

  print)*3! @entimetres to -nches*+

  print)*O! <eet to ?etres*+

  print)*S! ?etres to feet*+

  print)*L! W9itNn*+

print)*?easurement @onverter*+

stop <alse

while stop <alse:

  try:

  print?enu)+

  choice int)input)*Please make a selection: *++

  if choice L:

  stop True

  elif choice % or choice H L:

  print)*-nvalid entryC*+

  else:

  valueTo@onvert int)input)*Please enter the value to convert: *++

  convert)choice,valueTo@onvert+

  e9cept:

  print)*Please make a valid selection*+

Page 22: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 22/28

 Rajput [SIX PROGRAMMING ACTIVITES]

Page 23: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 23/28

 Rajput [SIX PROGRAMMING ACTIVITES]

Page 24: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 24/28

 Rajput [SIX PROGRAMMING ACTIVITES]

-ssues during development solution

•  This task was completed with

minimal error

• ?inor errors were corrected and

9ed

Evaluation

Averall this task was fairly simple and easy to complete!

 Task (: $crabble @alculator

-n $crabble, words are given scores dependent upon the characters that are in theword! Wach letter has a numeric value like this:

 ou have been asked to write a program that allows a user to enter a word andcalculates the value of that word! $o, XDello" would have a value of 0! -f a user needsto enter a double or triple letter score they enter the value after the letter e!g! if therst X" of Xhello" was a double letter score they would enter Xhel3lo"!

 ou will need to store all of the values for the alphabet in this task! They are:

Page 25: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 25/28

 Rajput [SIX PROGRAMMING ACTIVITES]

 ou may wish to use http://www!asciitable!com/ to help you solve this problem!

def validateBord)word+:

  isalid True

  inde9Afetter .

  for letter in word:

  asciialue ord)letter+

  if asciialue (L or asciialue H &.:

  if not )letter 3 or letter O+:

  print)*That is an invalid character*+

  isalid <alse

  return isalid

def get@haracteralue)c+:

  asciialueAf@har ord)c+

  char-nMlphabet asciialueAf@har 2 (L

  return int)letter6alue1char-nMlphabet4+

def spellable)word, rack+:

  word6count count6letters)word+

  rack6count count6letters)rack+

  return all) 1word6count1letter4 rack6count1letter4 for letter in word4 +

letter6alue 1%,O,O,3,%,S,3,S,%,0,L,%,O,%,%,O,%.,%,%,%,%,S,S,0,S,%.4

Page 26: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 26/28

 Rajput [SIX PROGRAMMING ACTIVITES]

letter6$core .

word6$core .

word6alid <alse

while not word6alid:

  word input)*Wnter a word: *+!upper)+

  word6alid validateBord)word+

for letter in word:

  letter6$core get@haracteralue)letter+

  word6$core 5 letter6$core

print)*That word scored *, word6$core, * points*+

Page 27: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 27/28

 Rajput [SIX PROGRAMMING ACTIVITES]

Page 28: Six Programming Tasks

8/10/2019 Six Programming Tasks

http://slidepdf.com/reader/full/six-programming-tasks 28/28

 Rajput [SIX PROGRAMMING ACTIVITES]

-ssues during development solution

•  The library for the values of the

letters in the alphabet were

diJcult at rst to gure out how

to format!

• Using online resources - was able

to teach my self how to use this

kind of function an implement it

in to my work!

Evaluation

Averall this was a successful task, but was the most diJcult as it involved some

features of python - was not familiar with, however - was still able to complete

the task successfully!