简体   繁体   中英

Moving from Python 3 to Python 2

print('10 -> 2 [bd], 2 -> 10 [db]')
answ=input('select db or bd : ')

if  answ == "db":
a=input('enter a digit')
x=int(a)
list1 = []

while (x):
    x%2
    x//2

    if x==0:
        break

I began creating this on python 3.2, but then I had to move on python 2.7.5 and I get the following error message:

Traceback (most recent call last):
  File "C:\Users\<file path>", line 3, in <module>
    answ=input('select db or bd : ')
  File "<string>", line 1, in <module>
NameError: name 'db' is not defined
>>> 

I really don't know hat is all about, worked pretty fine on python 3.2 (sorry for my bad english).

You need to use raw_input :

answ=raw_input('select db or bd : ')

input in Python 2.x evaluates input as real Python code.

Also, just a tip: these two lines:

x%2
x//2

don't do anything. Perhaps you meant:

x %= 2
x //= 2

In python 2 the equivalent to input is called raw_input

so line 2 should be answ=raw_input('select db or bd : ')

http://docs.python.org/2/library/functions.html#raw_input

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM