Python“ValueError:不完整的格式”在打印时(“stuff %” %“thingy”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34666335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python "ValueError: incomplete format" upon print("stuff %" % "thingy")
提问by Oughh
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!
我对这段代码的目标是,当您输入某个数字时,您将根据您输入的内容打印该数字和其他一些输出。出于某种原因,我在这里给出了错误“ValueError:不完整的格式”。它与%有关。错误是什么意思,我该如何解决?谢谢!
variable = "Blah"
variable2 = "Blahblah"
text = raw_input("Type some stuff: ")
if "1" in text:
print ("One %" % variable)
elif "2" in text:
print ("Two %" % variable2)
采纳答案by Chad S.
it's expecting another character to follow the %
in the string to tell it how to represent variable
in the string.
它期望%
在字符串中跟随另一个字符来告诉它如何variable
在字符串中表示。
use "One %s" % variable
or "One {}".format(variable)
to correct the issue.
使用"One %s" % variable
或"One {}".format(variable)
纠正问题。
回答by ?????
an easy way:
一个简单的方法:
print ("One " + variable)
回答by Ravi Gadhia
>>> variable = "Blah"
>>> '%s %%' % variable
'Blah %'
>>>