Creating time stamps using .format()
<p>Basically in this assignment we have to create a time, based on values of ran(hour,min):. Below I will post the code I have and then the error message I receive. Is my code just really bad or there something I'm missing? What does it mean by "Can't convert 'int' object to str implicitly.</p>

<pre><code>def show_time(hour,min):
if(hour &gt; 12):
    hour = hour -12
if(min &lt; 10):
    min = "0"+min
print("{hour}:{min}".format(show_time()))
</code></pre>

<hr>

<pre><code>Traceback (most recent call last):
  File "zyLabsUnitTestRunner.py", line 10, in &lt;module&gt;
    passed = test_passed(test_passed_output_file)
  File "/home/runner/local/unit_test_student_code/zyLabsUnitTest.py", line 33, in test_passed
    ans = show_time(hour,min)
  File "/home/runner/local/unit_test_student_code/main.py", line 15, in show_time
    min = "0"+min
TypeError: Can't convert 'int' object to str implicitly
</code></pre>

<p>The reason you are getting this error is because you are concatenating <code>"0"</code> (a string) with <code>min</code>, which is an integer.</p>

<pre><code>&gt;&gt;&gt; '0' + 3
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: Can't convert 'int' object to str implicitly
</code></pre>

<p>You would avoid this by casting <code>min</code> as a string:</p>

<pre><code>&gt;&gt;&gt; '0' + str(3)
'03'
</code></pre>

<p>The best solution, though, would be to save this kind of formatting for--you guessed it!--<code>str.format()</code>. It has a <a href="https://docs.python.org/2/library/string.html#format-string-syntax" rel="nofollow">rich syntax</a> that allows you to keep these transformations where they belong. Here is an example:</p>

<pre><code>&gt;&gt;&gt; '{:02d}'.format(3)
'03'
</code></pre>

<p>As it stands, your code has several issues:</p>

<ol>
<li>You are calling it without arguments in your code snippet.</li>
<li>You are using the variable <em>inside</em> the format function rather than in its arguments: you appear to be mixing up passing in keyword parameters like so:</li>
</ol>

<pre>
>>> '{a}:{b}'.format(**{'a':1, 'b':2})
'1:2'
</pre>

<ol start="3">
<li>You are not returning anything with the function. </li>
<li><code>min</code> is a bad name for a variable since it is a built-in function.</li>
</ol>

<p>Here is a much easier way of doing everything in one block:</p>

<pre><code>print("{}:{:02d}".format(hour % 12, minute))
</code></pre>

python