Asked  6 Months ago    Answers:  5   Viewed   40 times

I am learning python and have this error . I can figure out wherewhat the error is in the code. File "<string>", line 1, in <module>.

Name = ""
Desc = ""
Gender = ""
Race = ""
# Prompt user for user-defined information
Name = input('What is your Name? ')
Desc = input('Describe yourself: ')

When i run the program

it outputs What is your Name? (i input d )

this gives the error

Traceback (most recent call last):
  File "/python/chargen.py", line 19, in <module>
    Name = input('What is your Name? ')
  File "<string>", line 1, in <module>
NameError: name 'd' is not defined

This is an example code from Python 3 for Absolute Beginners.

 Answers

10

In Python 2.x, input() expects something which is a Python expression, which means that if you type d it interprets that as a variable named d. If you typed "d", then it would be fine.

What you probably actually want for 2.x is raw_input(), which returns the entered value as a raw string instead of evaluating it.

Since you're getting this behavior, it looks like you're using a 2.x version of the Python interpreter - instead, I'd go to www.python.org and download a Python 3.x interpreter so that it will match up with the book you're using.

Tuesday, June 1, 2021
 
JackTheKnife
answered 6 Months ago
27

python3 is not Python syntax, it is the Python binary itself, the thing you run to get to the interactive interpreter.

You are confusing the command line with the Python prompt. Open a console (Windows) or terminal (Linux, Mac), the same place you'd use dir or ls to explore your filesystem from the command line.

If you are typing at a >>> or In [number]: prompt you are in the wrong place, that's the Python interpreter itself and it only takes Python syntax. If you started the Python prompt from a command line, exit at this point and go back to the command line. If you started the interpreter from IDLE or in an IDE, then you need to open a terminal or console as a separate program.

Other programs that people often confuse for Python syntax; each of these is actually a program to run in your command prompt:

  • python, python2.7, python3.5, etc.
  • pip or pip3
  • virtualenv
  • ipython
  • easy_install
  • django-admin
  • conda
  • flask
  • scrapy
  • setup.py -- this is a script you need to run with python setup.py [...].
  • Any of the above together with sudo.

with many more variations possible depending on what tools and libraries you have installed and what you are trying to do.

If given arguments, you'll get a SyntaxError exception instead, but the underlying cause is the same:

>>> pip install foobar
  File "<stdin>", line 1
    pip install foobar
              ^
SyntaxError: invalid syntax
Tuesday, June 1, 2021
 
QuantumMechanic
answered 6 Months ago
75

The reload built-in function has been moved to importlib module in Python 3.4:

In [18]: from importlib import reload

In [19]: reload?
Reload the module and return it.

The module must have been successfully imported before.

As pointed out by @JPaget in comments reload() function has been moved from imp to importlib module in Python 3.4+. From what's new in Python 3.4:

The reload() function has been moved from imp to importlib as part of the imp module deprecation

Thursday, August 12, 2021
 
bimal
answered 4 Months ago
58

use the following to convert to a timestamp in python 2

int((mod_time.mktime(first_run.timetuple())+first_run.microsecond/1000000.0))

Sunday, August 22, 2021
 
waylaidwanderer
answered 4 Months ago
60

The reason that this line

year_choice = [i for i in range(100) if i > max_year]

works in Python 2 but not in Python 3 is that in Python 3 list comprehensions create a new scope, and the max_year class attribute isn't in that scope. In Python 2, a list comprehension doesn't create a new scope, it runs in the context of the surrounding code. That was originally done for performance reasons, but a lot of people found it confusing, so it was changed in Python 3, bringing list comprehensions into line with generator expressions, and set and dict comprehensions.

AFAIK, there is no simple way in Python 3 to access a class attribute inside a list comprehension that is running in the outer context of a class, rather than inside a method. You can't refer to it with Student.max_year, since at that point the Student class doesn't exist.

However, there really is no point having that list comprehension there anyway. You can create the list you want more compactly, and more efficiently. For example:

class Student(object):
    max_year = 18
    year_choice = list(range(max_year + 1, 100))

    def __init__(self, name):
        self.name = name
        print (self.year_choice)

Student('Blah')

output

[19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

That code produces the same output on Python 2 and Python 3.

I've changed the class signature to

class Student(object):

so that it creates a new-style class in Python 2 (in Python 3 all classes are new-style).


The reason that [i for i in range(max_year)] can get around this restriction is that an iterator is created from range(max_year) which is then passed as the argument to the temporary function which runs the list comprehension. So it's equivalent to this code:

class Student(object):
    max_year = 18
    def _listcomp(iterator):
        result = []
        for i in iterator:
            result.append(i)
        return result

    year_choice = _listcomp(iter(range(max_year + 1, 100)))
    del _listcomp

    def __init__(self, name):
        self.name = name
        print(self.year_choice)

Student('Blah')

Many thanks to Antti Haapala for this explanation.

Monday, October 11, 2021
 
Tito Sanz
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share