Home » Python Programming

Category Archives: Python Programming

Python Programming – Final Exam

You need to be registered and logged in to take this quiz. Log in

Python Programming – Week 10 Assignments

Python Programming Online Class

assignment

Week 10 Objectives
• Guessing Game Link found here

Assignment:

  • As I indicated in the Guess Game post, no quiz this week. I want you to take a good look at the article provided.

Python Program – Guess the number game

As we begin to wind down, I thought this week, we would have a Python program, and then review each part. This program is a guessing game. The player will attempt to guess the number (which is random and will always change), while being provided if their guess is high or low.

Let’s see the full program first, then break it down. Don’t be overwhelmed, we have actually covered most of this before.

# This is a guess the number game.
import random

guessesTaken = 0

print(‘Hello! What is your name?’)
myName = input()

number = random.randint(1, 20)
print(‘Well, ‘ + myName + ‘, I am thinking of a number between 1 and 20.’)

while guessesTaken < 6:
print(‘Take a guess.’) # There are four spaces in front of print.
guess = input()
guess = int(guess)

guessesTaken = guessesTaken + 1

if guess < number:
print(‘Your guess is too low.’) # There are eight spaces in front of print.

if guess > number:
print(‘Your guess is too high.’)

if guess == number:
break

if guess == number:
guessesTaken = str(guessesTaken)
print(‘Good job, ‘ + myName + ‘! You guessed my number in ‘ + guessesTaken + ‘ guesses!’)

if guess != number:
number = str(number)
print(‘Nope. The number I was thinking of was ‘ + number)

The first block of code calls for import random and assigns the variable guessTaken the value of zero. The import random will pull in the random function to be used a bit later.
import random
guessesTaken = 0
The second block of code is a simple print statement, and another variable, “myName” with the input. Recall this will take whatever the user values as their name and assign it to the variable myName.

print(‘Hello! What is your name?’)
myName = input()

This third black of code uses the function random, and creates a random number from 1 to 20. Then it assigns that value to the variable number. The second line prints out the string, “Well.. I am thinking of a number between 1 and 20”. The myName variable is used within the print statement.

number = random.randint(1, 20)
print(‘Well, ‘ + myName + ‘, I am thinking of a number between 1 and 20.’)

We are given a while loop in this next block of code. The guessesTaken count was originally set at zero in our first block of code. Now we see the while checks for less than 6 (we are given 6 tries). If less than six, then we are prompted with “Take a guess”. The users guess is put into the “guess” variable. The next line,  guess = int(guess), converts the value to a integer.

while guessesTaken < 6:
print(‘Take a guess.’) # There are four spaces in front of print.
guess = input()
guess = int(guess)

This block of code increases the variable guessesTaken by 1.

guessesTaken = guessesTaken + 1

This block of code does a check to see if the users number (he or she guessed) is lower than the random number value. If so, it prints out the information.

if guess < number:
print(‘Your guess is too low.’) # There are eight spaces in front of print.

This block of code does another check to see if the users number (he or she guessed) is higher than the random number value. If so, it prints out the information.

if guess > number:
print(‘Your guess is too high.’)

This block of code does another check to see if the users number (he or she guessed) is the same as the random number value. If so, it breaks out of the if statement.

if guess == number:
break

Once the above IF is done (either by printing out the information, or breaking out if the value is correct), it does a second check, and if the guess is equal too or correct, will convert the number of guessesTaken back to a string and print out your success and how many tries it took.

if guess == number:
guessesTaken = str(guessesTaken)
print(‘Good job, ‘ + myName + ‘! You guessed my number in ‘ + guessesTaken + ‘ guesses!’)

The final block of code does the same check and if incorrect, will convert the number of tries back to a string and inform you, how many times you tried and you are incorrect with the answer.

if guess != number:
number = str(number)
print(‘Nope. The number I was thinking of was ‘ + number)

 

This is a nice little program, using the random generator function, while loop, if statement, print function, count, and conversion to/from strings and integers. For this week, we will not have a quiz. Rather, I wanted you to see how many small pieces put together, can create a cool little program to play with.

 

Python Programming – Week 9 – Assignments

Python Programming Online Class

assignment

Week 9 Objectives
• Overview of For Loop/Range Link found here

Assignment:

  • Create one program using a For Loop and Range
  • Complete Week 9 Quiz

Python Programming – Week 9 – Quiz

You need to be registered and logged in to take this quiz. Log in

Python Programming – For Loop and Range

All sequences are made up of elements. In the below example, each letter is an element making up the sequence.
The for loop marches through (iterates) a sequence one element at a time.

# Code Example:
word = input(“Enter a word: “)
print (“\nHere’s each letter in your word:”)
for letter in word:
print(letter)

Range function within a For Loop:
You can use a for loop to count. This example provides a range(), telling the for loop how many times to count.

# Code Example:
print (“Counting:”)
for i in range(100):
print(i,””, end=””)

When you give range 3 values;
Start point, End point, and Number to count by

# Code Example:
print (“\n\nCounting by fives:”)
for i in range(0,50,5):
print(i,””, end=””)

# Counting Backwords

# Code Example:
print (“\n\nCounting by ones:”)
for i in range(10,0,-1):
print(i,””, end=””)

Python Programming – Week 8 Assignments

Python Programming Online Class

assignment

Week 8 Objectives
• Overview of Comments and Concatenation Link found here

Assignment:

  • Create one program using concatenation of string and number
  • Complete Week 8 Quiz

Week 8 – Python Programming – Quiz

Please enter your email:

1. Provide your name for this quiz.

2. Comments in Python are preceded with a

 
 
 

3. If using a hashtag in Python, this will

 
 
 

4. The following line of code will produce an error.

 
 
 

5. What is the definition of “the action of linking things together in a series”?

 
 
 

6. Write this code out and run it three times. Tell me the answer provided.

 

import random

# Choose a random element from a sequence
seq = [‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
x = random.choice(seq)

print (x)

 


Question 1 of 6

Python Programming – Comments and Concatenation

Comments

We covered the use of the hashtag in regards to commenting out individual lines of code in our Python programs.

 

comment-example1

The recent example of last week, we see our initial comments to identify the program, name of the programmer and date the program was written.

# While Loop Demonstration
# 3 year old why with count
# Matt Cole
# 9.22.16

I am seeing some inquire if there is a cleaner way to comment out sections of the code. This largely depends on the editor we are using. Up to last week, we have been using the provided GUI, via Python shell. Last week, we downloaded PyCharm Community Edition.

My comment is keep it simple. Do not comment out sections of code, and keep to the working hashtag.

 

 

Concatenation

Let’s look at a quick program regarding Print with Strings and Numbers.

combininginprint1

We see our comments in red. I want you to write the above code in your Python Idle, or PyCharm. Notice how the two out fine. We use the plus sign. Recall this is concatenating the two strings together, while also adding 3 and 6.

Concatenation is defined as “the action of linking things together in a series.”

In the above example, we are concatenating “The test is due ” with “in nine days”.  This is done with the plus sign.

combininginprint2png

We now see the same code, with the print statement “The test is due” + “in ” + 9 + “days”.  We would think this would work, but there is a problem. Python will only properly concatenate with the plus sign if it’s using all String.

Run this code again to see the results. Did you get the error, “TypeError: Can’t convert ‘int’ object to str implicitly”?

Python is confused for you using both String and numeric values. If we are combining both String and a number in our Print command, we will need to replace the plus sign with the comma.

combininginprint3

Notice the change in our program above. We replaced the plus with the comma. I want you to run it again and see if it now works. Interesting isn’t it.

 

 

 

Week 7 – Python Programming – Quiz

Please go to Week 7 – Python Programming – Quiz to view the test