computer programming
DESCRIPTION
Computer Programming. Week 2. Remember from last week, there are 2 modes that you can program with in PYTHON…. INTERACTIVE Mode >>> (has chevrons). SCRIPT Mode Used for writing larger programs and requires F5 to run. Open PYTHON and go to SCRIPT Mode - PowerPoint PPT PresentationTRANSCRIPT
COMPUTER PROGRAMMING
Week 2
Remember from last week, there are 2 modes that you can program with in PYTHON…
INTERACTIVE Mode>>> (has chevrons)
SCRIPT Mode
Used for writing larger programs and requires F5 to run.
Open PYTHON and go to SCRIPT Mode(Go to FILE menu, then click New Window)
print("Please type your name in")my_name = input ()
Write the following code into your PYTHON window
and save it as InteractiveProgram.py
What is the purpose of my_name in this program?
A variable is a place in the computer’s memory that holds a temporary value.Variable
DataWe can place data into a variable by ASSIGNING it to the variable.
Variable_Name DataAssignment
In Python, it looks like this Name = ‘Tom’
print("Please type your name in")my_name = input ()
input () is a useful function that prompts the user to enter some data.
It means that whatever the user types in will be
ASSIGNED to the variable my_name Save & Run the program (F5) to test it.
print("Please type your name in")my_name = input ()print(“Nice to meet you” ,my_name)
Edit your program and add the bottom line of code…
Python allows us to print a bunch of letters (known as a string) followed by a variable!
BUT! Make sure you separate the two things with a coma!
Re-test the program by running it.
print("Please type your name in")my_name = input ()print("Nice to meet you ", my_name)print("So ", my_name , " what is your favourite food?")favourite_food = input ()print("Ah, your favourite food is " , favourite_food)
Edit your program further by adding the last three lines of code.
Save it and then Run it (F5)
How does this program work?
print("Please type your name in")my_name = input ()print("Nice to meet you ", my_name)print("So ", my_name, " what is your favourite food?")favourite_food = input ()print("Ah, your favourite food is " , favourite_food)
Edit your program further by adding the last three lines of code. Save it and then Run it (F5)
QUESTIONS … you have 2 minutes! (Teacher > Click slide to start timer)1. Write down the name of a variable used in the above program2. How many variables are used in the above program3. What kind of character/symbol is used to assign values to a variable4. Write down an example of assignment5. Katie is writing her first program. It won’t run properly. Can you spot the
Syntax error?
Print(‘Hello World’) 2:001:591:581:571:561:551:541:531:521:511:501:491:481:471:461:451:441:431:421:411:401:391:381:371:361:351:341:331:321:311:301:291:281:271:261:251:241:231:221:211:201:191:181:171:161:151:141:131:121:111:101:091:081:071:061:051:041:031:021:011:000:590:580:570:560:550:540:530:520:510:500:490:480:470:460:450:440:430:420:410:400:390:380:370:360:350:340:330:320:310:300:290:280:270:260:250:240:230:220:210:200:190:180:170:160:150:140:130:120:110:100:090:080:070:060:050:040:030:020:01End
Task: Create a program in python that prompts the user for their name AND the names of their top 5 favourite films. Each film should be assigned to a separate variable. When run, the computer should output the following…
Hello [my_name], your top 5 favourite films are as follows:[favourite_film1][favourite_film2][favourite_film3][favourite_film4][favourite_film5]
Here is an example
Hello Tim, your top 5 favourite films are as follows:The Thin Red LineCity Of AngelsThe Breakfast ClubTerminator 2 Judgement DayThe Return of the King
If you can, do all these programs in SCRIPT mode (i.e. go to FILE, NEW WINDOW…then press F5 to run)
Save as ‘Favourite_Films.py’
Plenary ActivityFrom here:
http://www.computingacademy.org.uk/programming/introduction-programming-python
l