You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pretext/SimplePythonData/Input.ptx
+35-22Lines changed: 35 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -6,11 +6,12 @@
6
6
do is allow the user to enter any value they wish for the number of seconds. The program could then print the
7
7
proper result for that starting value.</p>
8
8
<p>In order to do this, we need a way to get <term>input</term> from the user. Luckily, in Python
9
-
there is a built-in function to accomplish this task. As you might expect, it is called <c>input</c>.</p>
9
+
there is a built-in function to accomplish this task. As you might expect, it is called <c>input</c>.
10
+
</p>
10
11
<programlanguage="python">
11
12
<input>
12
13
n = input("Please enter your name: ")
13
-
</input>
14
+
</input>
14
15
</program>
15
16
<p>The input function allows the user to provide a <term>prompt string</term>. When the function is evaluated, the prompt is
16
17
shown.
@@ -22,10 +23,10 @@ n = input("Please enter your name: ")
22
23
<input>
23
24
n = input("Please enter your name: ")
24
25
print("Hello", n)
25
-
</input>
26
+
</input>
26
27
</program>
27
28
<p>It is very important to note that the <c>input</c> function returns a string value. Even if you asked the user to enter their age, you would get back a string like
28
-
<c>"17"</c>. It would be your job, as the programmer, to convert that string into
29
+
<c>"17"</c>. It would be your job, as the programmer, to convert that string into
29
30
an int or a float, using the <c>int</c> or <c>float</c> converter functions we saw
30
31
earlier.</p>
31
32
<p>To modify our previous program, we will add an input statement to allow the user to enter the number of seconds. Then
<p>The variable <c>str_seconds</c> will refer to the string that is entered by the user. As we said above, even though this string may be <c>7684</c>, it is still a string and not a number. To convert it to an integer, we use the <c>int</c> function.
48
49
The result is referred to by <c>total_secs</c>. Now, each time you run the program, you can enter a new value for the number of seconds to be converted.</p>
0 commit comments