1.输入一个数值,判断是否为正数,负数,小数,以及字符串
1 import re 2 3 4 def is_number(num): 5 pattern = re.compile(r'^[-+]?[-0-9]d*.d*|[-+]?.?[0-9]d*$') 6 result = pattern.match(num) 7 if result: 8 return True 9 else: 10 return False 11 12 13 while True: 14 i = input("Enter Number:") 15 if i == 'q': 16 print('-----End----') 17 break 18 elif is_number(i): 19 if type(eval(i)) == int: 20 i = int(i) 21 if i > 0: 22 print('{} is positive number'.format(i)) 23 elif i < 0: 24 print('{} is negative number'.format(i)) 25 elif i == 0: 26 print('{} is Zero'.format(i)) 27 else: 28 print('{} is decimals'.format(i)) 29 else: 30 print('{} is a string'.format(i))
2:输入五位数组成一个列表,并求和
1 lists = [] 2 i = 0 3 while i < 5: 4 list_num = input('enter number:') 5 if list_num == 'q': 6 break 7 else: 8 list_num = int(list_num) 9 lists.append(list_num) 10 print(lists) 11 sum_num = sum(lists) 12 print('sum:{}'.format(sum_num)) 13 i += 1