Python Course Design Note 1 of 52

Flow Control - Reference Answers

Assignment 1

Write a user login authentication program. For example, the username is “Albert” and the password is “1”; ask the user to enter the username and password separately for authentication.

name = input('请输入用户名字:')
password = input('请输入密码:')
if name == 'Albert' and password == '1':
    print('Albert login success')
else:
    print('用户名或密码错误')

(The prompts mean “Please enter your username:” and “Please enter your password:”, and the error message means “Incorrect username or password”.)

Assignment 2

Write a program that prints user permissions; ask the user to enter a username for verification

Albert --> 超级管理员
tom  --> 普通管理员
jack,rain --> 业务主管
其他 --> 普通用户'

(Albert —> super administrator; tom —> regular administrator; jack, rain —> business supervisor; others —> regular user.)

'''
Albert --> 超级管理员
tom  --> 普通管理员
jack,rain --> 业务主管
其他 --> 普通用户
'''
name = input('请输入用户名字:')
if name == 'Albert':
    print('超级管理员')
elif name == 'tom':
    print('普通管理员')
elif name == 'jack' or name == 'rain':
    print('业务主管')
else:
    print('普通用户')

Exercise 3

Write a program that says whether to go to work based on the current day; ask the user to enter the day.

如果:今天是Monday,那么:上班
如果:今天是Tuesday,那么:上班
如果:今天是Wednesday,那么:上班
如果:今天是Thursday,那么:上班
如果:今天是Friday,那么:上班
如果:今天是Saturday,那么:出去浪
如果:今天是Sunday,那么:出去浪

(If today is Monday through Friday, then: go to work; if today is Saturday or Sunday, then: go out and have fun.)

# Approach 1:
today = input('>>: ')
if today == 'Monday':
    print('上班')
elif today == 'Tuesday':
    print('上班')
elif today == 'Wednesday':
    print('上班')
elif today == 'Thursday':
    print('上班')
elif today == 'Friday':
    print('上班')
elif today == 'Saturday':
    print('出去浪')
elif today == 'Sunday':
    print('出去浪')
else:
    print('''必须输入其中一种:
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
    ''')
# Approach 2:
today = input('>>: ')
if today == 'Saturday' or today == 'Sunday':
    print('出去浪')
elif today == 'Monday' or today == 'Tuesday' or today == 'Wednesday' \
        or today == 'Thursday' or today == 'Friday':
    print('上班')
else:
    print('''必须输入其中一种:
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
    ''')
# Approach 3:
today = input('>>: ')
if today in ['Saturday', 'Sunday']:
    print('出去浪')
elif today in ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']:
    print('上班')
else:
    print('''必须输入其中一种:
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday
    ''')

('必须输入其中一种:' means “You must enter one of the following:”.)

Exercise 4

Write a program that verifies user login in a loop. After the user is authenticated and logged in successfully, entering “q” exits the program. If authentication fails, the user is asked to log in again repeatedly; if the user still hasn’t logged in successfully after more than 3 attempts, the program exits.

"""
写一个循环验证用户登录的程序,用户认证登录成功后,
输入“q”退出程序,用户认证失败后重复让用户执行登录操作,
当用户重复次数超过3次仍没有登录成功,则退出程序。
"""
username = 'albert'
password = '1'
count = 0
tag = True
while tag:
    username_input = input('username>>:').strip()
    password_input = input('password>>:').strip()
    if count == 3:
        break
    if username_input == username and password_input == password:
        print('login successfully')
        while tag:
            cmd = input('>>:').strip()
            if cmd == 'q':
                tag = False
                break
    else:
        print('username or password is invalid')
        count += 1

Exercise 5

Write an age-guessing game that allows the user at most 3 attempts. After every 3 attempts, if the user still hasn’t guessed correctly, ask 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 directly.

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 = int(input('>>: '))
    if guess == Albert_age:
        print('you got it')
        break
    count += 1

Exercise 6

Print the following results as required:

  1. Use a while loop to output 1 2 3 4 5 6 8 9 10
  2. Compute the sum of all numbers from 1 to 100
  3. Output all odd numbers from 1 to 100
  4. Output all even numbers from 1 to 100
  5. Compute the sum of 1-2+3-4+5 … 99
# Question 1
# Approach 1
count = 1
while count <= 10:
    if count == 7:
        count += 1
        continue
    print(count)
    count += 1
# Approach 2
count = 1
while count <= 10:
    if count != 7:
        print(count)
    count += 1
# Approach 3
for i in range(1, 11):
    if i == 7:
        continue
    print(i)

# Question 2
res = 0
count = 1
while count <= 100:
    res += count
    count += 1
print(res)

# Question 3
count = 1
while count <= 100:
    if count % 2 != 0:
        print(count)
    count += 1

# Question 4
count = 1
while count <= 100:
    if count % 2 == 0:
        print(count)
    count += 1

# Question 5
res = 0
count = 1
while count <= 99:
    if count % 2 == 0:
        res -= count
    else:
        res += count
    count += 1
print(res)

Exercise 7

Print the following pyramid. The figure below 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 = 5
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()