-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4. Type List
More file actions
54 lines (44 loc) · 1.8 KB
/
Copy path4. Type List
File metadata and controls
54 lines (44 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#Assignment: Type List
Write a program that takes a list and prints a message for each element in the list, based on that element's data type.
Your program input will always be a list. For each item in the list, test its data type. If the item is a string, concatenate it onto a new string. If it is a number, add it to a running sum. At the end of your program print the string, the number and an analysis of what the list contains. If it contains only one type, print that type, otherwise, print 'mixed'.
Here are a couple of test cases. Think of some of your own, too. What kind of unexpected input could you get?
#input
l = ['magical unicorns',19,'hello',98.98,'world']
#output
"The list you entered is of mixed type"
"String: magical unicorns hello world"
"Sum: 117.98"
# input
l = [2,3,1,7,4,12]
#output
"The list you entered is of integer type"
"Sum: 29"
# input
l = ['magical','unicorns']
#output
"The list you entered is of string type"
"String: magical unicorns"#
mixed_list = ['magical unicorns',19,'hello',98.98,'world']
integer_list = [1,2,3,4,5]
string_list = ["Spiff", "Moon", "Robot"]
def identify_list_type(lst):
new_string = ''
total = 0
for value in lst:
if isinstance(value, int) or isinstance(value, float):
total += value
elif isinstance(value, str):
new_string += value
if new_string and total:
print "The array you entered is of mixed type"
print "String:", new_string
print "Total:", total
elif new_string:
print "The array you entered is of string type"
print "String:",new_string
else:
print "The array you entered is of number(float or int) type"
print "Total:", total
print identify_list_type(mixed_list)
print identify_list_type(integer_list)
print identify_list_type(string_list)