Data Types - Numbers and Strings - Reference Answers
Assignment 1
Write code: given the variable below, implement each feature as required
name = " alberT"
# 1 Remove the spaces on both sides of the value of the name variable, and print the result
# 2 Check whether the value of the name variable starts with "al", and print the result
# 3 Check whether the value of the name variable ends with "T", and print the result
# 4 Replace "l" with "p" in the value of the name variable, and print the result
# 5 Split the value of the name variable on "l", and print the result.
# 6 Convert the value of the name variable to uppercase, and print the result
# 7 Convert the value of the name variable to lowercase, and print the result
# 8 Print the 2nd character of the value of the name variable?
# 9 Print the first 3 characters of the value of the name variable?
# 10 Print the last 2 characters of the value of the name variable?
# 11 Print the index position of "e" in the value of the name variable?
# 12 Get a subsequence with the last character removed. E.g.: for albert, get alber
name = " alberT"
# 1 Remove the spaces on both sides of the value of the name variable, and print the result
print(name.strip())
# 2 Check whether the value of the name variable starts with "al", and print the result
print(name.startswith('al'))
# 3 Check whether the value of the name variable ends with "T", and print the result
print(name.endswith('T'))
# 4 Replace "l" with "p" in the value of the name variable, and print the result
print(name.replace('l', 'p'))
# 5 Split the value of the name variable on "l", and print the result.
print(name.split('l'))
# 6 Convert the value of the name variable to uppercase, and print the result
print(name.capitalize())
# 7 Convert the value of the name variable to lowercase, and print the result
print(name.lower())
# 8 Print the 2nd character of the value of the name variable?
print(name[2])
# 9 Print the first 3 characters of the value of the name variable?
print(name[:3])
# 10 Print the last 2 characters of the value of the name variable?
print(name[-2:])
# 11 Print the index position of "e" in the value of the name variable?
print(name.index('e'))
# 12 Get a subsequence with the last character removed. E.g.: for albert, get alber
print(name[:-1])
Assignment 2
Write a program where the user guesses an age (same as Assignment 2 in Chapter 3). Allow the user at most 3 attempts. After every 3 attempts, if they still haven’t guessed correctly, ask the user whether they want to keep playing. If they answer Y or y, let them guess 3 more times, and so on; if they answer N or n, exit the program. If they guess correctly, exit right away.
Requirement: fix the bug where the user’s input is not a number
Albert_age = 18
count = 0
while True:
if count == 3:
choice = input('继续(Y/N?)>>: ')
if choice == 'Y' or choice == 'y':
count = 0
else:
break
guess = input('>>: ')
if guess.isdigit():
guess = int(guess)
if guess == Albert_age:
print('you got it')
break
count += 1
else:
print('invalid operation')
Flowchart of the solution approach:

Assignment 3
Print the pyramid shape below. The figure is an isosceles triangle; each row always has two fewer stars than the row below it and sits directly above the row below (hint: use two nested for loops):
*
***
*****
*******
*********
"""
分析
#max_level=5
* #current_level=1,空格数=4,*号数=1
*** #current_level=2,空格数=3,*号数=3
***** #current_level=3,空格数=2,*号数=5
******* #current_level=4,空格数=1,*号数=7
********* #current_level=5,空格数=0,*号数=9
#数学表达式
空格数=max_level-current_level
*号数=2*current_level-1
"""
max_level = 50
for current_level in range(1, max_level + 1):
for i in range(max_level - current_level):
print(' ', end='') # Print multiple spaces in a row on one line
for j in range(2 * current_level - 1):
print('*', end='') # Print multiple spaces in a row on one line
print()
Requirements:
1, Write down your approach to solving the problem, in any form: written description, flowchart, or mind map.
2, After completing the assignment, take a screenshot & upload the image to check in.
3, Note: after checking in, share it in the study group and @ the teaching assistant to have your assignment code checked.