Loading...
What is this thing anyway?
tiny.cc/pythontaster
:)
Takes the magic of Python beyond the screen
Many large companies use Python for
Python teaches us many
it's not just for full-time programmers!
Get up and coding quickly
Free, cross-platform code editor
are just plain-text files
can be done directly from Komodo
Let's get coding!
print("Hello CS4HS!")
We make text show up on the screen by using the print function.
See if you can make your program create the following output
Hello CS4HS!
Today is Thursday.
I'm learning Python!
message = "Hello CS4HS"
print(message)
Variables allow us to store changeable information
under a consistent name.
Using the following variables
greeting = "Hello!"
introduction = "My name is"
name = "Alice"
luckyNumberText = "My lucky number is"
luckyNumber = 13
See if you can write a program which produces
Hello!
My name is Alice
My lucky number is 13
message = input("What is your message?")
print(message)
Receiving user input makes our program interactive.
We can store input in a variable.
Using the following starter code
greeting = "Hello!"
introduction = "My name is"
luckyNumberText = "My lucky number is"
# missing code goes here
print(greeting)
print(introduction, name)
print(luckyNumberText, luckyNumber)
See if you can modify the code to:
password = input("Please enter your password: ")
if password == "bunnies":
print("Success!")
else:
print("Access Denied")
If-else statements allow us to choose which piece of code to run, based on a value.
This is "Boolean logic" where a result is always either true or false.
Using the following starter code:
chosenNumber = input("Please enter a number between 1 and 10: ")
chosenNumber = int(chosenNumber)
# if less than one
print("Too low!")
# else if more than 10
print("Too high!")
# else
print("Perfect! Thank you.")
Change the code to output the correct response,
based on the user's input.
Make sure to check the boundary values (0, 1, 10, and 11)
for number in range(1, 6):
print("Number is", number)
Using a loop allows us to run a piece of code many times.
We can also keep track of how many times we have looped.
Use loops to create a star sail like this:
*
**
***
****
*****
******
*******
Hint: You can put a loop inside another loop!
Bonus: Can you make your loop draw a tree instead of a sail?
Bringing it all together
This is a calculator, but maybe not the kind of calculator you were expecting.
Your program must do the following:
What is your name? Mickey Mouse
What is the name of your crush? Minnie Mouse
You have a 75% match!
Your final running program might look something like this.
"Mickey Mouse" and "Minnie Mouse" are input provided by the user.
You will need to collect at least two pieces of information:
Make sure to store those values in variables.
If you're having trouble collecting input, try this:
# ask for input
firstPerson = input("What is your name? ")
secondPerson = input("What is your crush's name? ")
You will need to process the two names in some way. You could:
Make sure that the resulting number is a percentage.
If you're having trouble writing an algorithm, try this:
# count total number of letters
totalLetters = len(firstPerson) + len(secondPerson)
# count number of letters that are the same
matches = 0;
for letter in firstPerson:
if letter in secondPerson:
matches += 1
for letter in secondPerson:
if letter in firstPerson:
matches += 1
You will need to tell the user their result:
You are a 55% match!
Remember the result should be a "percentage match".
If you're having trouble printing the output, try this:
# output
percentage = int(matches / totalLetters * 100);
print(str(percentage) + "%" )
Loading...