2012-03-12 52 views
3

可以說人1具有用Python 3.x編寫的python可執行文件(mac)。 Person 1將所述文件發送給Person 2,Person 2也擁有一個mac,但只有Python 2.6.1。當Person 2運行該文件時,它會起作用嗎?在早期版本上運行的Python可執行文件

有人說,他們需要看到的代碼,所以:

#!/usr/bin/env python 
# -*- coding: UTF8 -*- 
topo1 = 0 
topo2 = 0 
print("This program helps compare two players: ") 
print("It only uses that player's stats from the previous two years to determine their worth in fantasy baseball") 
def complay1(): 
    global topo1 
    print("Enter in the first player's stats below") 
    homerun = input("Enter in the player's home run total from the most recent year: ") 
    sb = input("Enter in the player's stolen base total from the most recent year: ") 
    hit = input("Enter in the player's hit total from the most recent year: ") 
    walks = input("Enter in the player's walk total from the most recent year: ") 
    doubles = input("Enter in the player's doubles total from the most recent year: ") 
    rbi = input("Enter in the player's RBI total from the most recent year: ") 
    ba = input("Enter in the player's batting average from the most recent year, do not include a decimal point: ") 
    hitL = input("Enter in the player's hit total from the year before the most recent year: ") 
    homerunL = input("Enter in the player's home run total from the year before the most recent year: ") 
    age = input("Enter in the player's age: ") 
    gp = input("How many games did the player play last year?: ") 
    topo1 += int(homerun)*3 
    topo1 += int(sb)*2 
    topo1 += int(hit)/2.5 
    topo1 += int(walks)/4 
    topo1 += int(doubles) 
    topo1 += int(rbi)/3 
    topo1 += int(hitL)/15 
    topo1 += int(homerunL) 
    topo1/(int(gp)/4) 
    topo1 -= int(age) 
    topo1 += int(ba)/2 
    print(topo1, "is the total PLV+ for this player") 
def complay2(): 
    global topo2 
    print("Enter in the second player's stats below") 
    homerun = input("Enter in the player's home run total from the most recent year: ") 
    sb = input("Enter in the player's stolen base total from the most recent year: ") 
    hit = input("Enter in the player's hit total from the most recent year: ") 
    walks = input("Enter in the player's walk total from the most recent year: ") 
    doubles = input("Enter in the player's doubles total from the most recent year: ") 
    rbi = input("Enter in the player's RBI total from the most recent year: ") 
    ba = input("Enter in the player's batting average from the most recent year, do not include a decimal point: ") 
    hitL = input("Enter in the player's hit total from the year before the most recent year: ") 
    homerunL = input("Enter in the player's home run total from the year before the most recent year: ") 
    age = input("Enter in the player's age: ") 
    gp = input("How many games did the player play last year?: ") 
    topo2 += int(homerun)*3 
    topo2 += int(sb)*2 
    topo2 += int(hit)/2.5 
    topo2 += int(walks)/4 
    topo2 += int(doubles) 
    topo2 += int(ba)/2 
    topo2 += int(rbi)/3 
    topo2 += int(hitL)/15 
    topo2 += int(homerunL) 
    topo2/(int(gp)/4) 
    topo2 -= int(age) 
    topo1 += int(ba)/2 
    print(topo2, "is the total PLV+ for this player")  
complay1()  
complay2() 
if topo1 > topo2: 
    print("Player 1 is", ((topo1/topo2)*100)-100, "percent better") 
if topo2 > topo1: 
    print("Player 2 is", ((topo2/topo1)*100)-100, "percent better") 
+0

我跑了它,它給了我一個錯誤,說topo1沒有定義。我必須仔細研究它,看它是如何定義的。 – CoffeeRain 2012-03-12 20:34:15

+0

運行'3to2.py'就可以得到一個與python2兼容的版本(或多或少) – Daenyth 2012-03-12 20:34:45

+0

問題是你的complay2()有topo1而不是topo2。順便提一個程序的好主意! – CoffeeRain 2012-03-12 20:36:06

回答

4

大概不會,主版本的變化沒有向後兼容性。

編輯:對於您的代碼示例,它可能有效。腳本中2和3之間唯一改變的是print 2不是Python 2中的函數,這是不重要的,因爲print(x)與Python 2解釋器的print x相同,額外的括號不會造成傷害。

EDIT2:正如在不同的答案中所說,該部門也將打破。這是因爲int/int將導致Python 2中的int和Python 3中的float。這意味着5/2在Python 2中爲2,在Python 3中爲2.5。from __future__ import division解決了此問題。

2

這是不可能的完全一定沒有看到代碼,但也出現了2.x和3.x之間變化的很多,使它非常不可能奏效。

編輯:

該部門將打破它。將from __future__ import division放在頂部。另外,檢查是否存在raw_input,將其分配給input

+0

我把代碼放了! – Billjk 2012-03-12 20:29:24

0

可執行文件是什麼意思?我對python可執行文件的想法是將python捆綁在其中,所以最終用戶不需要安裝python來運行它。

如果你的意思只是.py,看着你發佈的代碼,它看起來是兼容的。

+0

我剛剛將python文件保存爲'filename',沒有任何擴展名,所以它在終端中運行... – Billjk 2012-03-12 20:29:59

相關問題