-3

This question is quite simple but I cannot find the good way to do that....

The thing is that I am trying to print on screen the progress of an operation...just for test purposes, but I am having an error when I try to print something like that: "Completed 30%"

The problem is that "%" is being taken as an argument here:

Here is the piece of the code

print "Completed: %s % " % (100*loops/totalLoops)

And here is the error:

print "Completed: %s %" % (100*loops/totalLoops)
ValueError: incomplete format

There should be an easy fix for that stupid thing...but I cannot find it.

codeKiller
  • 5,493
  • 17
  • 60
  • 115

1 Answers1

2

Use %% to print a single %:

print "Completed: %s %%" % (100*loops/totalLoops)

or use the new format:

print "Completed: {0} %".format(100. * loops / totalLoops)

which even allows you to print float as percents (without multiplying them with 100):

print "Completed: {0:.0%}".format(1. * loops / totalLoops)

For

loops = 2
totalLoops = 7

prints

'Completed: 29%'
eumiro
  • 207,213
  • 34
  • 299
  • 261
  • Thanks....stupid thing but it is making me crazy. I didn't know that about this new format. Thanks, in addtion to the answer I have learned something new!! – codeKiller Jan 30 '15 at 13:13