1

I have started a tutorial on python, have no prior programming expirience. Currently, I am doing exercise 5, where i have to transform inches into centimeters or vice versa and a problem occurs. I am using a 2.12 python and am not planning to switch for the time being of this tutorial. I am very annoyed and frustrated why this simple problem occurs and that i can't figure out why. Here is the code:

centimeter = 1
inch = centimeter * 2,54
converted_value = 10 * inch

print "i decided to convert 10 inches to centimeters. Results are astonishing. %d " % converted_value

I ran the exercise in Windows powershell and it reported back to me this:

"Traceback (most recent call last):
  File "vaja5.py", line 28, in <module>
    print "i decided to convert 10 inches to centimeters. Results are astonishing. %d" % converted_value
TypeError: not all arguments converted during string formatting"

Thank you for all the help in advance. I really appreciate it

2
  • i noticed there is a part cut off after the %. all it says there is %converted_value Commented May 13, 2020 at 9:17
  • 2
    Note that parentheses free print (print foo instead of print(foo)) shows that you are using Python2. Python2 is at its end of life. Please strongly consider to use Python3; if your tutorial is for Python2, strongly consider using a more up-to-date tutorial. Commented May 13, 2020 at 9:18

2 Answers 2

1

centimeter * 2,54 creates a tuple of 2 elements (2,54). The problem occurs when you try to provide 2 parameters to your string (the tuple is unpacked) which has only one placeholder (%d).

Change:

inch = centimeter * 2,54

to:

inch = centimeter * 2.54
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe it's more easy to start learning by running a code directly instead of calling it from PowerShell.
0

You have a typo:

inch = centimeter * 2,54

should be

inch = centimeter * 2.54

Your original syntax elaborates out to

inch = (centimeter * 2, 54)

so you end up assigning a tuple, which results in the formatting error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.