Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
newbie problem
#1
I am new to Python, I have a book entitled 'Automate the Boring Stuff' by Al Sweigart. I am going through it.

Why is myName = input()  not working? I thought input() should assign the string mike to myName.

The code is copied directly from the book, which was published in 2015. Python probably has not changed so much since then.

When I change to
Quote:myName = raw_input()
it works fine.

This is the output in a bash terminal. I am using Ubuntu 16.04

Quote:Hello world!
What is your name?
mike
Traceback (most recent call last):
 File "enteraVariableAndPrintIt.py", line 5, in <module>
   myName = input()
 File "<string>", line 1, in <module>
NameError: name 'mike' is not defined


------------------
(program exited with code: 1)
Press return to continue

# This program says hello and asks for my name.
print('Hello world!')
print('What is your name?')
# ask for their name
myName = input()
print('It is good to meet you, ' + myName)
print('The length of your name is:')
print(len(myName))
print('What is your age?')
# ask for their age
myAge = input()
print('You will be ' + str(int(myAge) + 1) + ' in a year.')
Thanks for any tips.
Reply
#2
The book is intended/using Python3 while you run the code using Python2.

In Python2 raw_input() takes user input and returns str, in Python3 that is input()
In Python2 input() takes input from user and try to evaluate it as code, in Python3 that is eval(input())

Most probably you have both version of python installed, but if not I would recommend to install Python3. Use Python3 (especially given that you are new to Python). usually, if both versions are installed you can invoke Python3 by using
python3 yourscript.py, not python yourscript.py
Reply
#3
Thanks a lot. I was using Geany and Bash. Now I have idle python 3.5 shell, everything seems to be working. Thanks for your help!
Reply
#4
Geany and IDLE are just IDEs.
I would assume you can change settings in Geany to work with any version of Python installed on your system
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  very newbie problem on text file zapad 2 215 Apr-12-2024, 06:50 PM
Last Post: zapad
  Newbie first problem cima_hurdle 8 3,439 Feb-09-2022, 10:02 PM
Last Post: cima_hurdle
  Newbie lists problem LedDiode 5 3,667 Dec-16-2018, 08:33 PM
Last Post: LedDiode
  Newbie Ubuntu script problem Kloontor 6 3,538 Sep-24-2018, 03:51 PM
Last Post: Kloontor
  Hey guys, help a newbie out with a list problem. sdezigner 2 2,669 Feb-15-2018, 06:06 PM
Last Post: sdezigner

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020