2012-03-12 5 views
3

Допустим, что у человека 1 есть исполняемый файл python (mac), написанный на Python 3.x. Лицо 1 отправляет указанный файл в 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

+0

Запустите '3to2.py' на нем, чтобы получить версию, совместимую с python2 (более или менее) – Daenyth

+0

Проблема в том, что у вас был topo1 вместо topo2 для complay2(). Отличная идея для программы, кстати! – CoffeeRain

ответ

4

Вероятно, нет, основные изменения версии не имеют обратной совместимостью.

EDIT: Для вашего примера кода это, вероятно, работает. Единственное, что изменилось между 2 и 3 в вашем скрипте, это то, что печать не является функцией в Python 2, что неважно, потому что print (x) совпадает с print x для интерпретатора Python 2, дополнительные скобки не повреждают.

EDIT2: Деление тоже сломается, как сказано в другом ответе. Это связано с тем, что int/int приведет к int в Python 2 и в float в Python 3. Это означает, что 5/2 - это 2 в Python 2 и 2.5 в Python 3. from __future__ import division исправляет это.

2

Это невозможно быть полностью уверен, не видя кода, но там было много изменений между 2.x и 3.x, что делает его крайне маловероятно, чтобы работать.

EDIT:

Деление разобьет. Поместите from __future__ import division наверху. Также проверьте, существует ли raw_input, назначив его input.

+0

Я положил код выше !! – Billjk

0

Что вы подразумеваете под исполнительным текстом? Мое представление о исполняемом файле python заключается в том, что в него входит пакет python, поэтому конечному пользователю не требуется устанавливать python для его запуска.

Если вы имеете в виду только .py, глядя на код, который вы опубликовали, он выглядит совместимым.

+0

Я только что сохранил файл python как «filename», без какого-либо расширения, поэтому он запускается в терминале ... – Billjk

Смежные вопросы