https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture7&feed=atom&action=historyI719 Fundamentals of Python/lecture7 - Revision history2024-03-28T18:46:00ZRevision history for this page on the wikiMediaWiki 1.41.0https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture7&diff=118706&oldid=prevEroman: Created page with "= Advanced control flow = == Task 1 == Make a function that returns the sum of all elements in an iterable <source lang="python">>>> my_sum_function([2,3,5]) # 2 + 3 + 5 10..."2017-03-19T12:24:21Z<p>Created page with "= Advanced control flow = == Task 1 == Make a function that returns the sum of all elements in an iterable <source lang="python">>>> my_sum_function([2,3,5]) # 2 + 3 + 5 10..."</p>
<p><b>New page</b></p><div>= Advanced control flow =<br />
<br />
== Task 1 ==<br />
<br />
Make a function that returns the sum of all elements in an iterable<br />
<br />
<source lang="python">>>> my_sum_function([2,3,5])<br />
# 2 + 3 + 5<br />
10</source><br />
== all, sum, any ==<br />
<br />
<code>all</code>, true if all elements are true<br /><br />
<code>any</code>, true if any elements are true<br />
<br />
== Task 2 ==<br />
<br />
Find if all elements are false<br />
<br />
<source lang="python">opposite_all([False, False])<br />
>>> True</source><br />
== Task 3 ==<br />
<br />
Implement <code>all</code> function with a for loop<br />
<br />
<source lang="python">my_all([True, True])<br />
>>> True</source><br />
== Break loops ==<br />
<br />
== Task 4 ==<br />
<br />
Implement <code>any</code> with a for loop, use a <code>break</code> statement<br />
<br />
== Error Handling ==<br />
<br />
* When to raise an exception<br />
* handling user input<br />
<br />
=== Example: Addition calculator ===<br />
<br />
this calculator is error prone. Users can put in a value that cannot be cast to an integer, which will cause this programs to raise an error.<br />
<br />
<source lang="python">def add_two_numbers(a, b):<br />
return int(a) + int(b)<br />
<br />
def main():<br />
a = input('Write a number: ')<br />
b = input('Write a number: ')<br />
result = add_two_numbers(a, b)<br />
print(result)<br />
<br />
<br />
if __name__ == "__main__":<br />
main</source><br />
giving an error like this on bad user input<br />
<br />
<pre>Write a number: a<br />
Write a number: 1<br />
Traceback (most recent call last):<br />
File &quot;calc.py&quot;, line 12, in &lt;module&gt;<br />
main()<br />
File &quot;calc.py&quot;, line 7, in main<br />
result = add_two_numbers(a, b)<br />
File &quot;calc.py&quot;, line 2, in add_two_numbers<br />
return int(a) + int(b)<br />
ValueError: invalid literal for int() with base 10: 'a'<br />
</pre><br />
==== With error handled ====<br />
<br />
<source lang="python">def add_two_numbers(a, b):<br />
return int(a) + int(b)<br />
<br />
def main():<br />
a = input('Write a number: ')<br />
b = input('Write a number: ')<br />
try:<br />
result = add_two_numbers(a, b)<br />
except ValueError:<br />
print('you must type a valid integer!')<br />
else:<br />
print(result)<br />
<br />
<br />
if __name__ == "__main__":<br />
main()</source><br />
==== With checking before ====<br />
<br />
<source lang="python">def add_two_numbers(a, b):<br />
return int(a) + int(b)<br />
<br />
<br />
def main():<br />
a = input('Write a number: ')<br />
b = input('Write a number: ')<br />
if a.isdigit() and b.isdigit():<br />
result = add_two_numbers(a, b)<br />
print(result)<br />
else: <br />
print('you must type a valid integer!')<br />
<br />
<br />
if __name__ == "__main__":<br />
main()</source><br />
= Splats =<br />
<br />
* dict as kwargs<br />
* list as args<br />
<br />
== Task: Sum of all arguments ==<br />
<br />
=== Solution ===<br />
<br />
<source lang="python">def sum_args(*args):<br />
return sum(args)<br />
<br />
if __name__ == "__main__":<br />
print(sum_args(1, 3, 5)) # 9<br />
</source><br />
=== Pass a list into the function ===<br />
<br />
<source lang="python">def sum_args(*args):<br />
return sum(args)<br />
<br />
if __name__ == "__main__":<br />
print(sum_args(*[1, 3, 5])) # 9<br />
</source></div>Eroman