I'm new to Python and the book I'm using was designed for an older version of Python and consequently there are some examples in the textbook that don't work in 3.1. My first problem is with the following simple example:
num1, num2 = input(" Please enter two numbers (num1, num2):")
I get the following error:
ValueError: too many values to unpack
What is the new way of doing this?

Also, is there a pdf document or a website that lists the full changes just to make it easier to learn Python?


Recommended Answers

All 4 Replies

There are quite few changes and improvements with Python version 3 from the older version 2. One of them is function input() which has replaced the older raw_input(). The original old version input() that could be used for numeric input is no longer. This is the reason you are getting an error.

Python3 contains a utility 2to3.py that can be used to convert Python2 code to Python3 code.

I suggest that you learn Python from newer books that have been written for Python3.

"Dive Into Python" Mark Pilgrim's Free online book, novice to pro, is updated constantly, and has been rewritten for Python3 (check appendix A for Py2 to Py3 differences)

Swaroop C.H. has rewritten his excellent beginning Python tutorial for Python3:

I forgot to include that I have modified the input to int(input()) but I still get the same error. Any thoughts?

input(" Please enter two numbers (num1, num2):")
gives you a string for instance "123,456" which function int() cannot convert that to an integer. In Python2 function input() actually used raw_input() and evaluated the string with the function eval(). However, eval() was dangerous, as it also evaluates command strings and some nasty user could wipe out the hard drive this way.

You could use eval() this way again to make the Python3 input work for you ...

num1, num2 = eval(input(" Please enter two numbers (num1, num2):"))

We are back to the dangers of eval(), so in Python3 you would be better off to write this code ...

num1 = int(input(" Please enter the first number: "))
num2 = int(input(" Please enter the second number: "))

Thanks for all your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.