Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.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_String - Fatal编程技术网

将整数更改为字符串;隐式地“?”;??python

将整数更改为字符串;隐式地“?”;??python,python,string,Python,String,我想知道是否有人能帮我。我对这个很陌生,我需要把我的输入转换成字符串 # Python program to calculate the Fibonacci numbers def fibR(n): if n == 1 or n == 2: return 1 else: return fibR(n - 1) + fibR(n - 2) # Request input from the user num = int(input("Please en

我想知道是否有人能帮我。我对这个很陌生,我需要把我的输入转换成字符串

# Python program to calculate the Fibonacci numbers
def fibR(n):
    if n == 1 or n == 2:
        return 1
    else:
        return fibR(n - 1) + fibR(n - 2)

# Request input from the user
num = int(input("Please enter the number in the Fibonacci sequence you wish to calculate: "))

#
if num == 1:
    print("The Fibonacci number you have requested is" + 1 + ".")
else :
    print("The Fibonacci number you have requested is" + fibR(num) + ".")

执行将数字转换为字符串的操作。

执行将数字转换为字符串的操作。

您已经正确地将
输入转换为
整数。但是,在您的
打印
语句中

print("The Fibonacci number you have requested is" + fibR(num) + ".")
..函数
fibR(num)
返回一个整数。当您尝试将返回的整数与字符串连接起来时,会导致错误。您需要做的是使用格式字符串:

print("The Fibonacci number you have requested is {}.".format(fibR(num))) 

您已经将
输入
正确地转换为
int
。但是,在您的
打印
语句中

print("The Fibonacci number you have requested is" + fibR(num) + ".")
..函数
fibR(num)
返回一个整数。当您尝试将返回的整数与字符串连接起来时,会导致错误。您需要做的是使用格式字符串:

print("The Fibonacci number you have requested is {}.".format(fibR(num))) 

else
语句中使用
%

print("The Fibonacci number you have requested is %d." % fibR(num))

else
语句中使用
%

print("The Fibonacci number you have requested is %d." % fibR(num))
如果你

您将了解到Python的制作者是如何思考的

显式比隐式好

因此,在Python中,您可以像在两个
print
语句中一样,只“添加”
string
s和
int
s。或者使用返回值
str(fibR(num))
显式地将数字转换为字符串,或者,更好的方法是查看。关于如何使用它的一些例子已经在这里的其他答案中给出了。

如果您

import this
您将了解到Python的制作者是如何思考的

显式比隐式好

因此,在Python中,您可以像在两个
print
语句中一样,只“添加”
string
s和
int
s。或者使用返回值
str(fibR(num))
显式地将数字转换为字符串,或者,更好的方法是查看。这里的其他答案已经给出了一些如何使用它的示例。

只需使用
str()
方法:

print("The Fibonacci number you have requested is" + str(fibR(num)) + ".")
只需使用
str()
方法:

print("The Fibonacci number you have requested is" + str(fibR(num)) + ".")

您的输入已经是字符串。您将其转换为
int
。您搜索过“TypeError:无法连接'str'和'int'对象”吗?在
n==1
情况下,为什么避免调用
fibR
函数?它不会导致1的性能问题。(但对于大的
n
,它会显示出来。)您的输入已经是字符串了。您将其转换为
int
。您搜索过“TypeError:无法连接'str'和'int'对象”吗?在
n==1
情况下,为什么避免调用
fibR
函数?它不会导致1的性能问题。(这将是大型
n
)非常感谢!!!我真的在努力提高自己。哦,顺便说一句,是她,不是他。非常感谢你!!!我真的在努力提高自己。哦,顺便说一句,是她,不是他。