Saturday, August 15, 2015

Intro to Programming with Python - Tutorial 9 - Exercise 2

This is our second exercise for tutorial 9 (https://youtu.be/rW-iqRxP6Cs)

In this exercise we have to redo the exercise from tutorial 8 using a while loop.

So for this exercise:
  • Initialize an empty grades dictionary variable.
  • Prompt the user for the number of students in the class. 
  • Convert the number of students to an integer
  • Use a WHILE loop to allow the user to enter each student name and grade and add them to the grades dictionary.
  • When finished print the grades dictionary back to the user in a friendly manner. You can use the same for loop to pretty print the dictionary as used in tutorial 8
Here is a sample output of running this program:


Here is the code that accomplishes this:
# Ask the user how many students they have in their class
numOfStudents = int(input('How many students do you have in your class? '))
# Create an empty grades dictionary
grades = {}
# Here is how we loop N times using a while loop
num = 0
while num < numOfStudents:
name = input('Enter student name: ')
# Read the mark and convert it to int
mark = int(input('Enter student mark: '))
grades[name] = mark
# Now we increment the num variable by 1
num += 1
# Now pretty print
print('Your class grades are as follows: ')
for name in grades:
print(' ', name, '-->', grades[name])

Intro to Programming with Python - Tutorial 9 - Exercise 1

In the 9th tutorial in our introduction to programming using Python series we present the while loops.
The video tutorial can be found here:
https://youtu.be/rW-iqRxP6Cs

Our first exercise for this tutorial is to create a number adder/averager.
When you run your code it should prompt the user to enter numbers until the user says 'Done'
Then it should display the number of numbers the user entered, the total and average for them.

Here is a screen shot of what you should see when you run your program:


The code to accomplish this is as follows:
# First we create variables for the number of numbers and the total
numberOfNumbers = 0
total = 0
# Now we loop until the user says Done
while True:
userInput = input('Please enter a number or "Done" if finished: ')
if userInput == 'Done':
break
num = int(userInput)
# Note total += num is the same as saying total = total + num
total += num
numberOfNumbers += 1
# We have to check if user entered no numbers.
# If we do not check and we divide (total / numberOfNumbers) we would
# be dividing by zero and Python would complain
if numberOfNumbers == 0:
print('You did not enter any numbers')
else:
avg = total / numberOfNumbers
print('You entered %s numbers. Total is %s and average is %s' % (numberOfNumbers, total, avg))

Sunday, August 2, 2015

Intro to Programming with Python - Tutorial 8 - Exercise

In the 8th tutorial in the introduction to programming with python series we introduced for loops.
The video tutorial can be found here:
https://www.youtube.com/watch?v=b8R9ysVd1sg

The exercise for this tutorial is to make the previous exercise, for tutorial 7, more flexible.
For this exercise:

  • Initialize an empty grades dictionary variable.
  • Prompt the user for the number of students in the class. 
  • Convert the number of students to an integer
  • Use a for loop to allow the user to enter each student name and grade and add them to the grades dictionary.
  • When finished print the grades dictionary back to the user in a more user friendly manner than what we did for the last exercise (See sample program output below)
Here is a sample output of running the program:


Here is the program itself for your reference:
# Ask the user how many students they have in their class
numOfStudents = int(input('How many students do you have in your class? '))
# Create an empty grades dictionary
grades = {}
# Here is our for loop.
# Remember using the range(numOfStudents)
# we will execute the loop that many times
for index in range(numOfStudents):
name = input('Enter student name: ')
# Read the mark and conver it to an int
mark = int(input('Enter student mark: '))
# Add the name -> mark key value pair to the grades dictionary
grades[name] = mark
# Now pretty print
print('Your class grades are as follows: ')
for name in grades:
print(' ', name, '-->', grades[name])


Intro to Programming with Python - Tutorial 7 - Exercise 1

In tutorial 7 in the intro to programming with python series we covered python dictionaries.
The YouTube tutorial can be found here:

The exercise for this tutorial is to build a dictionary for class grades.
Initialize the grades dictionary with five names and marks (You pick names and marks)

Then write a code block that will ask the user to enter a name. 
If name is found in the grades dictionary tell the user what his/her grade is. Then ask if they wish to change the grade. If answer is 'Yes' prompt them to enter a new grade and replace the old one in the dictionary.
If the name is NOT found in the grades dictionary prompt the user to enter a mark and add the name and associated mark to the grades dictionary.

Repeat this FIVE times (Copy and paste this code block)

When done tell the user what the grades dictionary looks like.

Here is a sample output from running the program.


Here is the program:
# Define the five key value pairs in our initial grades dictionary
# You can separate the key value pairs onto individual lines for clarity
grades = {
'John Smith' : 80,
'Jane Austin' : 82,
'Mohammed Ali' : 86,
'Janet Jackson' : 76,
'Bob Marley' : 92
}
# Now ask the user, five times
name = input('Enter a name: ')
if name in grades:
print('We have %s with a mark of %s' % (name, grades[name]))
replaceMark = input('Do you wish to change the mark (Yes/No)? ')
if replaceMark == 'Yes':
mark = int(input('Enter the new mark: '))
grades[name] = mark
else:
print('We do not have %s in our grades dictionary' % name)
mark = int(input('Enter a new mark for %s: ' % name))
grades[name] = mark
name = input('Enter a name: ')
if name in grades:
print('We have %s with a mark of %s' % (name, grades[name]))
replaceMark = input('Do you wish to change the mark (Yes/No)? ')
if replaceMark == 'Yes':
mark = int(input('Enter the new mark: '))
grades[name] = mark
else:
print('We do not have %s in our grades dictionary' % name)
mark = int(input('Enter a new mark for %s: ' % name))
grades[name] = mark
name = input('Enter a name: ')
if name in grades:
print('We have %s with a mark of %s' % (name, grades[name]))
replaceMark = input('Do you wish to change the mark (Yes/No)? ')
if replaceMark == 'Yes':
mark = int(input('Enter the new mark: '))
grades[name] = mark
else:
print('We do not have %s in our grades dictionary' % name)
mark = int(input('Enter a new mark for %s: ' % name))
grades[name] = mark
name = input('Enter a name: ')
if name in grades:
print('We have %s with a mark of %s' % (name, grades[name]))
replaceMark = input('Do you wish to change the mark (Yes/No)? ')
if replaceMark == 'Yes':
mark = int(input('Enter the new mark: '))
grades[name] = mark
else:
print('We do not have %s in our grades dictionary' % name)
mark = int(input('Enter a new mark for %s: ' % name))
grades[name] = mark
name = input('Enter a name: ')
if name in grades:
print('We have %s with a mark of %s' % (name, grades[name]))
replaceMark = input('Do you wish to change the mark (Yes/No)? ')
if replaceMark == 'Yes':
mark = int(input('Enter the new mark: '))
grades[name] = mark
else:
print('We do not have %s in our grades dictionary' % name)
mark = int(input('Enter a new mark for %s: ' % name))
grades[name] = mark
print('Here is the updated grades dictionary: ', grades)

Monday, July 27, 2015

Intro to Programming with Python - Tutorial 6 - Exercise 1

This is the solution to the exercise for the Intro to Programming with Python 6th tutorial covering lists.

The tutorial can be found here
https://youtu.be/2hdob7gw7r8

Exercise Description:
The exercise is to build a program that will do the following:
1 - Prompt the user to enter his/her four course names
2 - Print the course list back to the user
3 - Ask the user if they wish to change one of the courses (Yes, No)
4 - If they answer 'Yes':
    4.1 - Ask user which course number they wish to change (1,2,3,4)
    4.2 - Prompt the user to enter the new course name
    4.3 - Print the new course list to the user
5 - If they answer 'No':
    5.1 - Tell the user their course list did not change

Here is a sample output when you run the program you will create:


Bonus:
- Accept Y, Yes, yes, No, no, N
- Check if user entered less than 1 or more than 4 and if they did tell them they should enter only numbers from 1 to 4

Here is a sample output when you run the bonus version of the program:


Here is the solution to the exercise:
# Remember lines starting with pound sign are comments
# First prompt the user to enter his/her four courses
c1 = input('Enter your first course: ')
c2 = input('Enter your second course: ')
c3 = input('Enter your third course: ')
c4 = input('Enter your forth course: ')
# Now create the list out of the four courses
courses = [c1, c2, c3, c4]
# Print the course list.
print ('Your course list is: ', courses)
# Ask the user if he/she wishes to change one of the courses
changeCourse = input('Do you wish to change one of your courses(Yes/No)? ')
# If user answers yes check which course
if changeCourse == 'Yes':
courseNumber = int(input('Which course do you want to change (1,2,3,4)? '))
# Remember, we asked user to enter 1 to 4 so we have to subtract one
index = courseNumber -1
newCourse = input('Enter the new course name you wish to take: ')
courses[index] = newCourse
print('Your new course list is: ', courses)
else:
print('Your course list did not change')


And here is the solution including the bonus checks:
# Remember lines starting with pound sign are comments
# First prompt the user to enter his/her four courses
c1 = input('Enter your first course: ')
c2 = input('Enter your second course: ')
c3 = input('Enter your third course: ')
c4 = input('Enter your forth course: ')
# Now create the list out of the four courses
courses = [c1, c2, c3, c4]
# Print the course list.
print ('Your course list is: ', courses)
# Ask the user if he/she wishes to change one of the courses
changeCourse = input('Do you wish to change one of your courses(Yes/No)? ')
# If user answers yes check which course
# Remember a string is a sequence so we can go changeCourse[0] to check first letter
# so we check first character if it is a y in any case or a 1
if changeCourse[0].upper() == 'Y' or changeCourse == '1':
courseNumber = int(input('Which course do you want to change (1,2,3,4)? '))
# We can have if blocks within if blocks
# Check if user entered something less than 1 or more than 4
if courseNumber < 1 or courseNumber > 4:
print('Sorry but you must enter a number between 1 and 4')
else:
# Remember, we asked user to enter 1 to 4 so we have to subtract one
index = courseNumber -1
newCourse = input('Enter the new course name you wish to take: ')
courses[index] = newCourse
print('Your new course list is: ', courses)
else:
print('Your course list did not change')

Monday, July 20, 2015

Intro to programming with Python - Tutorial 5, Exercise 1

This is the solution to the simple calculator exercise in the fifth tutorial in the introduction to programming with Python series.
The tutorial can be found here:
https://www.youtube.com/watch?v=-I1QcEA8a7A

For this exercise you have to write a python program that will prompt the user for an operation (add, subtract, multiply, divide) then prompt the user for two numbers and perform the operation on them and display the result to the user.

Here is a sample output when you run your calculator python program (call it calc.py):


The program itself should be simple enough. You can copy this code into a python file (calc.py) and execute it.
Please note that in python lines that start with the pound sign are comment lines.
Comment lines give information about the code but do not affect how it executes. Python will ignore them.

# First, prompt the user for the operation he/she wishes to execute
op = input('What operation do you wish to execute?(add, subtract, multiply, divide): ')
# Now prompt the user for the two numbers to execute the operation on
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))
# Use if/elif to execute the desired operation
if op == 'add':
print('Your result is: ', num1 + num2)
elif op == 'subtract':
print('Your result is: ', num1 - num2)
elif op == 'multiply':
print('Your result is: ', num1 * num2)
elif op == 'divide':
print('Your result is: ', num1 / num2)
else:
# It seems the user entered an operation other than the supported ones. Give error message.
print('Sorry, I do not recognize this operation')
view raw calc.py hosted with ❤ by GitHub