Python practice example

Keywords: Python

31. Please input the first letter of the day of the week to judge the day of the week. If the first letter is the same, continue to judge the second letter.

Program analysis: it is better to use case statements. If the first letter is the same, then judge the second letter with case statements or if statements.

letter = input('Please input:')
if letter == 'S':
    print('Please input second letter:')
    letter = input('Please input:')
    if letter == 'a':
        print('Saturday')
    elif letter == 'u':
        print('Sunday')
    else:
        print('Date error')

elif letter == 'F':
    print('Friday')
elif letter == 'M':
    print('Monday')
elif letter == 'T':
    print('Please input second letter')
    letter = input('Please input:')
    if letter == 'u':
        print('Tuesday')
    elif letter == 'h':
        print('Thursday')
    else:
        print('Date error')

elif letter == 'W':
    print('Wednesday')
else:
    print('Date error')

 

32. Output the values of the list in reverse order.

#python 3.7

a = ['one', 'two', 'three']
for i in a[::-1]:
    print(i)

 

33. Comma separated list.

#python 3.7

L = [1, 2, 3, 4, 5]
s = ','.join(str(n) for n in L)
print(s)

 

34. Practice function call.

#python 3.7

def hello_world():
    print('hello world')

def three_hellos():
    for i in range(3):
        hello_world()

if __name__ == '__main__':
    three_hellos()

 

35. Text color setting.

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
print(bcolors.WARNING + "Color font for warnings?" + bcolors.ENDC)

 

36. Find the prime number within 100.

#python 3.7

lower = int(input('Minimum value of input interval:'))
upper = int(input('Maximum value of input interval:'))

for num in range(lower, upper + 1):
    if num > 1:
        for i in range(2, num):
            if (num % i) == 0:
                break
        else:
            print(num)

 

 

 

reference material:

1. 100 Python cases

2. Text color setting: https://blog.csdn.net/jacson_bai/article/details/71032462

Posted by lena_k198 on Sun, 01 Dec 2019 16:44:51 -0800