My goal with this code is that when you put in a certain number, you will get printed the number and some other output, based on what you typed. For some reason, what I have here gives the error "ValueError: incomplete format". It has something to do with the %. What does the error mean, and how do I fix it? Thanks!
variable = "Blah"
variable2 = "Blahblah"
text = raw_input("Type some stuff: ")
if "1" in text:
print ("One %" % variable)
elif "2" in text:
print ("Two %" % variable2)
>>> variable = "Blah"
>>> '%s %%' % variable
'Blah %'
>>>
Python is expecting another character to follow the %
in the string literal to tell it how to represent variable
in the resulting string.
Instead use
"One %s" % (variable,)
or
"One {}".format(variable)
to create a new string where the string representation of variable
is used instead of the placeholder.
an easy way:
print ("One " + variable)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With