Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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
如何在将Python2中生成的文件传输到Python3时修复代码_Python - Fatal编程技术网

如何在将Python2中生成的文件传输到Python3时修复代码

如何在将Python2中生成的文件传输到Python3时修复代码,python,Python,如何修复代码: Subtotal = input("What is your Subtotal? ") input_tax = input('What is the Tax? Please enter as a decimal ') input_tip = input('What is the Tip? Please enter as a decimal ') Tax = float(input_tax) * float(Subtotal) Tip = float(input_tip) * f

如何修复代码:

Subtotal = input("What is your Subtotal? ")
input_tax = input('What is the Tax? Please enter as a decimal ')
input_tip  = input('What is the Tip? Please enter as a decimal ')
Tax = float(input_tax) * float(Subtotal)
Tip = float(input_tip) * float(Subtotal)
Total = float(Tip) + float(Tax) + float(Subtotal)
print ("Your Total is $") + str(Total)
Traceback (most recent call last):
  File "C:\Python27\Tip_and_Tax_Calculator.py", line 7, in <module>
    print ("Your Total is $") + str(Total)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'`
当错误消息为:

Subtotal = input("What is your Subtotal? ")
input_tax = input('What is the Tax? Please enter as a decimal ')
input_tip  = input('What is the Tip? Please enter as a decimal ')
Tax = float(input_tax) * float(Subtotal)
Tip = float(input_tip) * float(Subtotal)
Total = float(Tip) + float(Tax) + float(Subtotal)
print ("Your Total is $") + str(Total)
Traceback (most recent call last):
  File "C:\Python27\Tip_and_Tax_Calculator.py", line 7, in <module>
    print ("Your Total is $") + str(Total)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'`
回溯(最近一次呼叫最后一次):
文件“C:\Python27\Tip\u and\u Tax\u Calculator.py”,第7行,在
打印(“您的总数为$”)+str(总数)
TypeError:不支持+:“NoneType”和“str”的操作数类型`

只需将
str(总计)
移到括号内即可。在Python中,3是一个函数。因此,现在Python正在尝试将
print
(始终为
None
)的返回值添加到
str(Total)
(这是一个字符串)

Subtotal = input("What is your Subtotal? ")
input_tax = input('What is the Tax? Please enter as a decimal ')
input_tip  = input('What is the Tip? Please enter as a decimal ')
Tax = float(input_tax) * float(Subtotal)
Tip = float(input_tip) * float(Subtotal)
Total = float(Tip) + float(Tax) + float(Subtotal)
print ("Your Total is $") + str(Total)
Traceback (most recent call last):
  File "C:\Python27\Tip_and_Tax_Calculator.py", line 7, in <module>
    print ("Your Total is $") + str(Total)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'`
因此,将最后一行更改为:

Subtotal = input("What is your Subtotal? ")
input_tax = input('What is the Tax? Please enter as a decimal ')
input_tip  = input('What is the Tip? Please enter as a decimal ')
Tax = float(input_tax) * float(Subtotal)
Tip = float(input_tip) * float(Subtotal)
Total = float(Tip) + float(Tax) + float(Subtotal)
print ("Your Total is $") + str(Total)
Traceback (most recent call last):
  File "C:\Python27\Tip_and_Tax_Calculator.py", line 7, in <module>
    print ("Your Total is $") + str(Total)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'`
print(“您的总数为$”+str(总数))

此外,在Python 3中,您可以使用更优雅的方式格式化字符串:

Subtotal = input("What is your Subtotal? ")
input_tax = input('What is the Tax? Please enter as a decimal ')
input_tip  = input('What is the Tip? Please enter as a decimal ')
Tax = float(input_tax) * float(Subtotal)
Tip = float(input_tip) * float(Subtotal)
Total = float(Tip) + float(Tax) + float(Subtotal)
print ("Your Total is $") + str(Total)
Traceback (most recent call last):
  File "C:\Python27\Tip_and_Tax_Calculator.py", line 7, in <module>
    print ("Your Total is $") + str(Total)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'`
  • 对于Python>=3.0:字符串方法:

    Subtotal = input("What is your Subtotal? ")
    input_tax = input('What is the Tax? Please enter as a decimal ')
    input_tip  = input('What is the Tip? Please enter as a decimal ')
    Tax = float(input_tax) * float(Subtotal)
    Tip = float(input_tip) * float(Subtotal)
    Total = float(Tip) + float(Tax) + float(Subtotal)
    print ("Your Total is $") + str(Total)
    
    Traceback (most recent call last):
      File "C:\Python27\Tip_and_Tax_Calculator.py", line 7, in <module>
        print ("Your Total is $") + str(Total)
    TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'`
    
  • 对于Python>=3.6::

    Subtotal = input("What is your Subtotal? ")
    input_tax = input('What is the Tax? Please enter as a decimal ')
    input_tip  = input('What is the Tip? Please enter as a decimal ')
    Tax = float(input_tax) * float(Subtotal)
    Tip = float(input_tip) * float(Subtotal)
    Total = float(Tip) + float(Tax) + float(Subtotal)
    print ("Your Total is $") + str(Total)
    
    Traceback (most recent call last):
      File "C:\Python27\Tip_and_Tax_Calculator.py", line 7, in <module>
        print ("Your Total is $") + str(Total)
    TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'`
    

print(“您的总数是$”+str(Total))
。这是因为在Python 3中
print
返回
None
。您可以使用Python>=3.6执行
print(您的总数是${Total}')