kidsprogramming

 

Sequence_Python

Page history last edited by Michael 1 yr ago

Sequence

 

The simplest computer programs are those which require only a number of separate sequenced steps. In this section you will plan and write computer programs for problems requiring a solution of this kind where you will get data as input from the user, do some processing on that data and then output information back to the user.

 

Typical of these programs are as follows:

 

Get some numbers and perform a calculation

 

# Age calculation program written in Python

#input

year = int(raw_input("What year is it now? eg 2007 -> "))

born = int(raw_input("What year were you born in? eg 1999 -> "))

 

# processing

age = year - born

 

#output

print 'You are ' + repr(age) + ' years old'

 

Note:

  • Lines starting with a # are comments and are ignored by the Python interpreter
  • To read data from the keyboard, the raw_input function is used. Input data is read as a string.
  • To convert the data to an integer, the int function is used
  • To display the results, the print function is used. The integer value (age) must be converted to a string and concatenated with the rest of the text.

 

Get a character such as Y for yes, or N for N

 

# Get a character program written in Python

#input

gender = raw_input("Are your male (M) or female (F) -> ")

 

# processing

# none

 

#output

print 'You entered %1s' %(gender)

 

Note:

A variable can be printed using  formatting. The %1s is embedded into the string to be printed - this indicates that there a 1 character string to be printed, the value of which is in the list that follows the string.

 

Get some words and make them into a sentence

 

# Get a word program written in Python

#input

noun = raw_input("Enter a noun -->")

verb = raw_input("Enter a verb -->")

adjective = raw_input("Enter an adjective -->")

 

# processing

# none

 

#output

print 'The ' + adjective  + ' ' + noun + ' ' + verb + ' on the window ledge'

print 'The %s %s on the %s rug' %(noun, verb, adjective) 

 

Note:

Two variations of the print statement have been used to demonstrate functionality

 

Exercises

  1. Churchland Fencing Contractors charges customers $74 per metre for Trim-Lock fencing and $49 per metre to erect it. Churchland Fencing has contracted you to write a computer program using PHP that will calculate the cost of supplying and installing a new fence. Design and write the program that could be used for this purpose.
  2. Write a program that allows the user to enter his/her given name, middle initial, surname, gender and age, and then display it in the following order - Surname, Given Name Initial <age> <gender>.  eg enter Bill and then Jones, and then C, and then 16 and then M to display Jones, Bill C <16> <M>
  3. <Please add more>

 

 


 

[Python_Tutorial] [Interation_Python] [Decision_Making_Python] [Arrays_Python] [Files_Python]  

Comments (0)

You don't have permission to comment on this page.