“%” python给了我“不是在字符串格式化期间转换的所有参数” [英] '%' python is giving me a "not all arguments converted during string formatting"
问题描述
我已经下载了此文本要求我提供的确切版本,但是'%'给了我一个问题。
I've downloaded the exact version this text asked me to however the '%' is giving me a problem.
x = raw_input('Enter an integer:')
if x%2 == 0:
print ''
print 'Even'
else:
print ''
print 'Odd'
print 'Done with conditional'
只有'%'运算符给我一个问题。任何其他运算符和脚本都可以正常运行。我已经读过该版本很重要,因为%也可以是字符串插值运算符,但是就像我说的那样,我相信我所拥有的版本与我的文本匹配。
It is only the '%' operator giving me a problem. Any other operator and the script runs fine. I've read that the version matters because '%' can also be 'string interpolation operator' but like I said I believe the version I have matches my text. WHAT DO I DO NEXT?
谢谢,是的,我刚刚读了第一本关于编程的书。
Thanks and yes I have pretty much just picked up my first book on programming.
推荐答案
raw_input()
函数返回字符串,而不是整数。要将其更改为整数,必须调用 int()
函数:
x = int(raw_input('Enter an integer:'))
请注意,如果输入数字以外的任何内容,都会引发错误。
Note that an error will be raised if you input anything other than a number.
python认为您正在做什么,试图将字符串格式与%
运算符。由于您输入的字符串中没有%
,因此会出现错误并非在格式化字符串时转换了所有参数。
What python thinks you're doing is trying to use string formatting with the %
operator. As there are no "%"
in the string you input, the error "not all arguments converted during string formatting" is raised.
这篇关于“%” python给了我“不是在字符串格式化期间转换的所有参数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!