1. Input-Processing

advertisement
Programming Problems: Input – Processing – Output
Variables:
Using “variables” is the way that the computer remembers information in your
program. Every time the user enters some information, the computer must
remember it. Every time the computer needs to count, it must remember the value
of the count. Every time the computer does calculations, it must remember the
values that are used in the calculation and the result of the calculation.
This information is stored in the computer’s memory. In your program, the place in
the computer’s memory that the information is stored is labeled with a name (like ‘x’,
‘num’, ‘name’, ‘count’, ‘cost’, etc.) This name is called a “variable name”. (“variable”
means that it can change … as opposed to a “constant”, which is a defined value
that does not change, such as PI.)
In Python, to create a variable all you have to do is assign a value to the variable.
Python will figure out what type of information it is and make the right type of varia
This means that you put a certain value into the place in memory that the
variable name is labeling. For example:
count = 1
temperature = 37.5
username = 'Kevin Wood'
Showing Output:
Until we get into creating Graphical User Interfaces (GUIs), we are just going to use
the console for input and output. The simplest way to show something on the
console is to use the print command. For example:
print
print
print
print
'Hello world'
count
'The temperature is', temperature
'Hello', username + ', welcome to Python.'
Notice that Python automatically puts a space between the listed items (where the
comma separates the items). You can avoid the space between strings (text values)
by using a + sign to concatenate strings. In a program, you can put a comma at the
end of the print statement to stop it from automatically going to the next line.
Getting Input:
To get input from the user, you can use the raw_input() function. This will ask the
user to enter some information in the console. The raw_input() function can take a
string parameter, which is a prompt that it will print before waiting for the user to
respond. For example:
username = raw_input('Enter username: ')
To get a number, you must convert the text from the raw_input() function to a
number value using the int() or float() function. For example:
age = int(raw_input('How old are you? '))
temperature = float(raw_input('Enter the temperature Celsius: '))
Programming Problems: Input – Processing – Output
Please use the variable names shown in red.
1. Write a program called ipo1.py which asks the user to type in their name
(userName) and then the computer tells them to have a nice day. For example, if
the user types in Fred, then the program would say:
Have a nice day Fred.
2. Write a program called ipo2.py which asks the user for a number, and then tells
them the square of the number. If the user types in 12, the program would say:
The square is 144
3. Write a program called ipo3.py which asks the user for a price and then tells
them the cost with 15% tax added on. If the user types in 5.00, the program would
say:
The cost with tax is $5.75
4. Write a program called ipo4.py which asks the user for a price and then tells
them the sale price after a 10% discount. If the user types in 4.50, the program
would say:
The sale price is $4.05
5. Write a program called ipo5.py which asks the user for a unitCost and a q
uantity of units ordered. The totalCost is then displayed. If the user types
2.50 and 10, the program would show:
10 units will cost $25.00
6. Temperature Converter - Save as: ipo6.py
Write a program to convert temperatures from Fahrenheit to Celsius.
Use two variables: cels and fahr. Search the Internet to find the
formulae to convert back and forth.
7. Percent Calculator - Save as: ipo7.py
Write a program to ask the user for their mark on a test and what the
test was out of, and then display the mark as a percentage. Use the
variables mark, outOf, and percent.
8. Area Calculator - Save as: ipo8.py
Write a program to calculate the area of a circle. Use the variables:
radius and area
You can get the value of pi from the math module. At the top of your
program you need to add the statement:
import math
Then you can get pi using math.pi. For example:
print '2 x pi =',2*math.pi
9. Pythagorean Formula - Save as: ipo9.py
Write a program to calculate the hypotenuse of a triangle using the
Pythagorean formula. Use the variables: side1, side2 and
hypotenuse
The square root function is part of the math module, so remember to
import it.
Example:
print 'The square root of 16 is',math.sqrt(16)
Download