xonecas.io
387
Pe

python-exercices / commit 4aca21e

Add ex_16. Exercise has an error in the question

The solution is updated to the correction. See this PR:
https://github.com/zhiwehu/Python-programming-exercises/pull/111/files

xonecas2020-08-24 4aca21eaf8d3af2353200710268b793e1f2d7b7c parent d22a552

1 file changed

level_two/ex_16.py+25−0
@@ -0,0 +1,25 @@
1+#!/usr/bin/env python3
2+
3+#
4+# This exercise is wrong:
5+# https://github.com/zhiwehu/Python-programming-exercises/pull/111/files
6+# Given the input in the question the output should be: 1,9,25,49,81
7+#
8+
9+# Question 16
10+# Level 2
11+
12+# Question:
13+# Use a list comprehension to square each odd number in a list. The list is input by a sequence
14+# of comma-separated numbers.
15+# Suppose the following input is supplied to the program:
16+# 1,2,3,4,5,6,7,8,9
17+# Then, the output should be:
18+# 1,3,5,7,9
19+
20+# Hints:
21+# In case of input data being supplied to the question, it should be assumed to be a console input.
22+
23+values = input('Enter a list of numbers separated by commas\n')
24+numbers = [str(int(x) ** 2) for x in values.split(",") if int(x) % 2 != 0]
25+print(",".join(numbers))