<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=I719_Fundamentals_of_Python%2Flecture3v3</id>
	<title>I719 Fundamentals of Python/lecture3v3 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=I719_Fundamentals_of_Python%2Flecture3v3"/>
	<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture3v3&amp;action=history"/>
	<updated>2026-06-21T16:49:13Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture3v3&amp;diff=120339&amp;oldid=prev</id>
		<title>Eroman: Created page with &quot; = Lecture 3 =  Input, Handling Errors, Unit Testing  == Input from console ==  You can prompt the user for input using a function called &lt;code&gt;input&lt;/code&gt;.  on python2, this...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture3v3&amp;diff=120339&amp;oldid=prev"/>
		<updated>2017-04-20T22:42:07Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; = Lecture 3 =  Input, Handling Errors, Unit Testing  == Input from console ==  You can prompt the user for input using a function called &amp;lt;code&amp;gt;input&amp;lt;/code&amp;gt;.  on python2, this...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt; = Lecture 3 =&lt;br /&gt;
&lt;br /&gt;
Input, Handling Errors, Unit Testing&lt;br /&gt;
&lt;br /&gt;
== Input from console ==&lt;br /&gt;
&lt;br /&gt;
You can prompt the user for input using a function called &amp;lt;code&amp;gt;input&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
on python2, this is called &amp;lt;code&amp;gt;raw_input&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;input&amp;lt;/code&amp;gt; is a function that stops execution until the user hits &amp;#039;enter&amp;#039;, and the value they enter is the output of the the &amp;lt;code&amp;gt;input&amp;lt;/code&amp;gt; function.&lt;br /&gt;
&lt;br /&gt;
== Task 1 ==&lt;br /&gt;
&lt;br /&gt;
Write a program that takes two numbers from the user and prints the numbers added together.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;quot;&amp;amp;quot;&amp;amp;quot;&lt;br /&gt;
Addition calculator&lt;br /&gt;
&lt;br /&gt;
Write a script that takes in two numbers, and adds them together.&lt;br /&gt;
&amp;amp;quot;&amp;amp;quot;&amp;amp;quot;&lt;br /&gt;
&lt;br /&gt;
number_1 = int(input(&amp;#039;Write a number: &amp;#039;))&lt;br /&gt;
number_2 = int(input(&amp;#039;Write a number: &amp;#039;))&lt;br /&gt;
print(number_1 + number_2)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== &amp;lt;code&amp;gt;try&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;except&amp;lt;/code&amp;gt; ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;try&amp;lt;/code&amp;gt; creates a block that can run code that may raise an error. If an error is raised, the code in &amp;lt;code&amp;gt;except&amp;lt;/code&amp;gt; will run. If an error is NOT raised, then the code in &amp;lt;code&amp;gt;else&amp;lt;/code&amp;gt; will run&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;number_1 = input(&amp;#039;Write a number: &amp;#039;)&lt;br /&gt;
number_2 = input(&amp;#039;Write a number: &amp;#039;)&lt;br /&gt;
&lt;br /&gt;
try:&lt;br /&gt;
    number_1 = int(number_1)&lt;br /&gt;
    number_2 = int(number_2)&lt;br /&gt;
&lt;br /&gt;
except ValueError:&lt;br /&gt;
    print(&amp;#039;You must enter valid integers&amp;#039;)&lt;br /&gt;
else:&lt;br /&gt;
    print(number_1 + number_2)&amp;lt;/source&amp;gt;&lt;br /&gt;
== Exceptions ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;class NotIntegerError(Exception):&lt;br /&gt;
    pass&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
number_1 = input(&amp;#039;Write a number: &amp;#039;)&lt;br /&gt;
number_2 = input(&amp;#039;Write a number: &amp;#039;)&lt;br /&gt;
&lt;br /&gt;
if not number_1.isdigit():&lt;br /&gt;
    raise NotIntegerError(&amp;#039;{0} is not a number&amp;#039;.format(number_1))&lt;br /&gt;
&lt;br /&gt;
if not number_2.isdigit():&lt;br /&gt;
    raise NotIntegerError(&amp;#039;{0} is not a number&amp;#039;.format(number_2))&lt;br /&gt;
&lt;br /&gt;
number_1 = int(number_1)&lt;br /&gt;
number_2 = int(number_2)&lt;br /&gt;
print(number_1 + number_2)&amp;lt;/source&amp;gt;&lt;br /&gt;
== Unit testing ==&lt;br /&gt;
&lt;br /&gt;
See https://wiki.itcollege.ee/index.php/I719_Fundamentals_of_Python/testing&lt;br /&gt;
&lt;br /&gt;
== Arguments from commandline ==&lt;br /&gt;
&lt;br /&gt;
The standard library has sys which give us access to the command line arguments executed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import sys&lt;br /&gt;
 &lt;br /&gt;
if &amp;#039;-h&amp;#039; in sys.argv or &amp;#039;--help&amp;#039; in sys.argv:&lt;br /&gt;
    print(&amp;#039;Sorry I can\&amp;#039;t help you :(&amp;#039;)&amp;lt;/source&amp;gt;&lt;br /&gt;
But argparse from the standard library is much easier if we want to use the commandline arguments.&lt;br /&gt;
&lt;br /&gt;
== Opening a file, printing every line ==&lt;br /&gt;
&lt;br /&gt;
The file path is passed into the script in the command line. run this script using &amp;lt;code&amp;gt;python3 my_script.py --help&amp;lt;/code&amp;gt; and a help message will show. run this script with &amp;lt;code&amp;gt;python3 my_script path/to/my/file.txt&amp;lt;/code&amp;gt; to read &amp;lt;code&amp;gt;file.txt&amp;lt;/code&amp;gt; line by line.&lt;br /&gt;
&lt;br /&gt;
NOTE: you must run this from the commandline, not from your IDE&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&amp;lt;/source&amp;gt;&lt;br /&gt;
== Task 2 ==&lt;br /&gt;
&lt;br /&gt;
Count the occurence of words in the file.&lt;br /&gt;
&lt;br /&gt;
example output&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;{&amp;#039;banana&amp;#039;: 1, &amp;#039;carrot&amp;#039;: 9, &amp;#039;pumpkin&amp;#039;: 2, &amp;#039;cabbage&amp;#039;: 7}&amp;lt;/pre&amp;gt;&lt;br /&gt;
example solution&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import argparse&lt;br /&gt;
&lt;br /&gt;
parser = argparse.ArgumentParser(description=&amp;#039;Count unique words in a file&amp;#039;)&lt;br /&gt;
parser.add_argument(&amp;#039;path&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
args = parser.parse_args()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
my_file = open(args.path, &amp;#039;r&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
result = {}&lt;br /&gt;
for line in my_file:&lt;br /&gt;
    clean_line = line.strip()&lt;br /&gt;
    result[clean_line] = result.get(clean_line, 0) + 1&lt;br /&gt;
&lt;br /&gt;
my_file.close()&lt;br /&gt;
print(result)&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Eroman</name></author>
	</entry>
</feed>