Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 并非所有参数都在字符串格式化期间转换。。没有%的变量_Python_Python 3.x_Typeerror - Fatal编程技术网

Python 并非所有参数都在字符串格式化期间转换。。没有%的变量

Python 并非所有参数都在字符串格式化期间转换。。没有%的变量,python,python-3.x,typeerror,Python,Python 3.x,Typeerror,如果你知道科拉兹猜想是什么,我想做一个计算器。我想让x作为我的输入,这样我就不必每次尝试新号码时都更改x的号码并保存 我得到下面的错误 TypeError:并非所有参数都在字符串格式化过程中转换' 在7号线 请帮助解决问题。问题在于您需要用户输入: x = input() y = 1 print (x) while 1 == y: if x == 1: y == y + 1 elif x % 2 == 0: #even x = x // 2 print (x) else

如果你知道科拉兹猜想是什么,我想做一个计算器。我想让x作为我的输入,这样我就不必每次尝试新号码时都更改x的号码并保存

我得到下面的错误

TypeError:并非所有参数都在字符串格式化过程中转换' 在7号线


请帮助解决问题。

问题在于您需要用户输入:

x = input()
y = 1 
print (x)
while 1 == y:
if x == 1:
    y == y + 1
elif x % 2 == 0: #even
    x = x // 2
    print (x)
else:
    x = 3 * x + 1
    print (x)
现在
x
是一个
str
。因此,在这一行:

x = input()
用作字符串插值运算符

但是,您提供的
输入没有格式说明符,因此:

>>> mystring = "Here goes a string: %s and here an int: %d" % ('FOO', 88)
>>> print(mystring)
Here goes a string: FOO and here an int: 88
>>>
现在,它将执行您想要的操作:

x = int(input())

请注意,while循环下面的代码没有正确缩进。
>>> "a string with no format specifier..." % 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>>
x = int(input())
>>> x = int(input("Gimme an int! "))
Gimme an int! 88
>>> x % 10
8
>>>