简体   繁体   中英

Python - not all arguments converted during string formatting

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

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

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.

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