为了在Python 2.7中工作,我应该修改代码中的哪些内容?

为了在Python 2.7中工作,我应该修改代码中的哪些内容?,python,python-2.7,python-3.5,Python,Python 2.7,Python 3.5,credit\u num=input(“输入信用卡号:”)。替换(“,”) tot1=0 tot2=0 对于信用证中的i_num[-1::-2]: tot1+=int(i) 对于信用证中的i_num[-2::-2]: tot2+=总和(整数(x)表示str中的x(整数(i)*2)) 雷姆=(总量1+总量2)%10 如果rem==0: 打印(“输入的数字有效。”) 其他: 打印(“输入的数字无效。”)将input()替换为raw\u input(),并将打印函数调用更改为打印语句,或者您可以按照@

credit\u num=input(“输入信用卡号:”)。替换(“,”)
tot1=0
tot2=0
对于信用证中的i_num[-1::-2]:
tot1+=int(i)
对于信用证中的i_num[-2::-2]:
tot2+=总和(整数(x)表示str中的x(整数(i)*2))
雷姆=(总量1+总量2)%10
如果rem==0:
打印(“输入的数字有效。”)
其他:
打印(“输入的数字无效。”)
input()
替换为
raw\u input()
,并将打印函数调用更改为打印语句,或者您可以按照@BrenBarn的建议,从
\uu future\uuuuu
导入
打印函数,例如:

from __future__ import division, print_function

credit_num = raw_input("Enter the credit card number: ").replace(" ", "")
tot1 = 0 
tot2 = 0 

for i in credit_num[-1::-2]:
    tot1 += int(i)

for i in credit_num[-2::-2]:
    tot2 += sum(int(x) for x in str(int(i)*2))

rem = (tot1 + tot2) % 10

if rem == 0:
    print("The entered numbers are valid.")
else:
    print("The entered numbers are not valid.")
input()
替换为
raw\u input()
,并将打印函数调用更改为打印语句,或者您可以按照@BrenBarn的建议从
\uuuuu future\uuuuuuuu\ucode>导入
print\u函数,例如:

from __future__ import division, print_function

credit_num = raw_input("Enter the credit card number: ").replace(" ", "")
tot1 = 0 
tot2 = 0 

for i in credit_num[-1::-2]:
    tot1 += int(i)

for i in credit_num[-2::-2]:
    tot2 += sum(int(x) for x in str(int(i)*2))

rem = (tot1 + tot2) % 10

if rem == 0:
    print("The entered numbers are valid.")
else:
    print("The entered numbers are not valid.")

如果您希望在Python2.x和Python3.x中使用相同的脚本,我建议使用“six”模块和以下代码。(请注意,前面添加的两行是我所做的唯一更改。)


如果您希望在Python2.x和Python3.x中使用相同的脚本,我建议使用“six”模块和以下代码。(请注意,前面添加的两行是我所做的唯一更改。)


print调用实际上将按原样工作,因为括号只是作为多余的分组。但是一个更好的解决方案是在开始时将uuu future uuuu导入print_函数中的
,允许您使用Python 3风格的print函数。print调用实际上会按原样工作,因为括号只是作为多余的分组。但是一个更好的解决方案是在开始时将
从_未来_导入打印_函数
,允许您使用Python 3风格的打印函数。