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:
And here is the solution including the bonus checks:
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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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') |