[Tutor] Beginning String Problems

boB Stepp robertvstepp at gmail.com
Wed Jan 1 20:01:42 EST 2020


Greetings!

On Wed, Jan 1, 2020 at 6:15 PM William Dickey <wpdickey at usc.edu> wrote:
>
> I am learning Python via an online video "Beginning Python" by William
> Fiset. I am at the end of chapter 2 and I am supposed to be inputting a
> string that has an output of a persons full name in  this order *First M.
> Last*, but when I copied what he was doing it is not working. Here is what
> I input:
>
> first_name = str(input("Please enter your first name: "))
> middle_name = str(input("Please enter your middle name: "))
> last_name = str(input("Please enter your last name: "))

N.B.:  Using str() on the above 3 lines is unnecessary as input()
always returns a string from the user's input.

> first_name = first_name.capitalize()
> middle_name = middle_name.capitalize()
> last_name = last_name.capitalize()
>
> name_format = "{first} {middle:.1s} {last}"
> print(name_format.format(first=first_name, middle=middle_name,
> last=last_name))
>
> and here is what I get when I open it in the command line
>
> I enter the file location and I get:
> Please enter your first name:
>
> I enter william and I get this error:
>
> Traceback (most recent call):
>     File "C:\xxx\xxx\xxx\xxx", line 3, in <module>
>        first_name = str(input("Please enter your first name: "))
>     File "<string>", line 1, in <module>
> NameError: name 'william' is not defined
>
> I am using Windows 10 64 bit, Atom text editor, Python 3.4.

I am just a hobbyist programmer.  But when I copy and pasted your code
as given in your email into a file and then ran it in the terminal
(I'm on Linux, using Python 3.7.5) everything worked well:

bob at Dream-Machine1:~/Projects/Tutor_Help$ python3 ./test.py
Please enter your first name: boB
Please enter your middle name: Vincent
Please enter your last name: Stepp
Bob V Stepp

So I am puzzled.  Are you certain that you do not have your name
"william" written on the top line of your Python file?  If yes, then
Python will see that as an undefined variable name, since you wouldn't
have assigned anything to it.  That is my best guess anyway.

BTW, for a first time poster I feel you did a rather good job asking
your question!

HAPPY NEW YEAR TO YOU AND YOURS!!!
-- 
boB


More information about the Tutor mailing list