Python 用整数和浮点数连接字符串?

Python 用整数和浮点数连接字符串?,python,concatenation,Python,Concatenation,各位嘉宾:, 我刚刚开始学习用python编写代码,我正在构建一个非常基本的百分比计算器,以便在头脑中运转 我在运行程序的成功流程时遇到问题: #Percentage Calculator print('Enter value of percent: ') #prompt user for input of percent value percent = input() #gain user input about percent *stored as 'str' percent = int(

各位嘉宾:, 我刚刚开始学习用python编写代码,我正在构建一个非常基本的百分比计算器,以便在头脑中运转

我在运行程序的成功流程时遇到问题:

#Percentage Calculator

print('Enter value of percent: ') #prompt user for input of percent value
percent = input() #gain user input about percent *stored as 'str'
percent = int(percent) #store and convert user input into an 'int' from 'str' for use in line 11

print('Enter value of percentaged number: ') #prompt user for input of percentaged number value
percentagedNum = input() #gain user input on percentaged number *stored as 'str'
percentagedNum = int(percentagedNum) #store and convert value from 'str' into 'int'

answer = percent / percentagedNum #calculate percentage formula

print(percent + '% of ' + percentagedNum + ' is ' + answer) #prompt user with answer
此外,回溯:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1438, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Custom/PycharmProjects/PercentageCalculator/main", line 12, in <module>
    print(percent + '% of ' + percentagedNum + ' is ' + answer)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
回溯(最近一次呼叫最后一次):
文件“C:\Program Files\JetBrains\PyCharm Community Edition 2020.1\plugins\python ce\helpers\pydev\pydevd.py”,第1438行,在_exec中
pydev_imports.execfile(文件、全局、局部)#执行脚本
文件“C:\Program Files\JetBrains\PyCharm Community Edition 2020.1\plugins\python ce\helpers\pydev\\u pydev\u imps\\u pydev\u execfile.py”,第18行,在execfile中
exec(编译(内容+“\n”,文件,'exec'),全局,loc)
文件“C:/Users/Custom/PycharmProjects/PercentageCalculator/main”,第12行,在
打印(百分比+%'+percentagedNum+'是'+答案)
TypeError:不支持+:“int”和“str”的操作数类型
我觉得在最后的print()函数调用中混合字符串、整数和浮点是一个连接问题


如果你们能看到这一点,我们将不胜感激,并感谢你们为帮助社区所做的一切。非常喜欢。

在您的
print
语句中,确保使用
str()
str(百分比)
等将包含数字的变量转换为字符串


实际上,当您开始使用Python时,这是一个典型的错误。

您需要首先将数字转换为字符串。您可以显式地执行此操作:

print(str(百分比)+'%of'+str(percentagedNum)+'是'+str(答案))
或者您可以让Python f-strings来处理它:

print(f'{percentage}{percentage}}的{percent}%为{answer}')
您尝试的操作不起作用的原因是,
+
操作符的结果不同,具体取决于所给的操作。如果任意一侧都有字符串,则会连接:

>“foo”+“bar”
“foobar”
如果两边都有整数,则将它们相加:

>5+3
8.

当你混合输入类型时,它不确定它应该做什么。

啊,我明白了。在将它们转换为整数数据类型之后,我必须在公式计算之后将它们转换回字符串数据类型。经验教训:连接时始终保持数据类型相同。再次感谢你,大卫。谢谢你的澄清,Pac0。我发现我必须将
percent=int(percent)
的数据类型更改回
percent=str(percent)
,以便在最终的
print()
函数中正确连接,而不会出现运行时错误。“非常感谢,先生。”格伦:这也适用于其他变量。并不是说我不喜欢“改变变量的类型”,通常像
variable=sometype(variable)
。从长远来看,这可能会导致更多的混乱。您只需在如下方式打印时将其格式化为字符串:
print(str(percent)+'%of'+str(percentagedNum)+'是'+str(answer))
。当然,这可能有点难读。David的答案是可读性更强的代码!(这很重要,我们为像你和你的同事这样的人编写代码,比为计算机编写代码更重要!)是的。将数据类型更改应用于其他变量。对于解决方案,F字串听起来更方便。然而,学习串联限制总是很受欢迎的。真的很感谢你们。