我正在尝试执行一个 Python 程序(用 Python 3.x 编写)来比较两个数字。IDE:Eclipse JAR:jython-standalone-2.5.3.jar
当我运行程序时,在控制台中的 Eclipse 中它说:“请输入第一个整数:” 输入整数后程序不会进一步执行。可能是什么问题?注意:它在 Python IDLE 中运行良好。
JAVA CODE:(ExperimentWithPython.java)
===============
import org.python.core.PyException;
import org.python.util.PythonInterpreter;
public class ExperimentWithPython{
public static void main(String[] args) throws PyException{
try
{
PythonInterpreter.initialize(System.getProperties(), System.getProperties(),new String[0]);
PythonInterpreter interp = new PythonInterpreter();
interp.execfile("D:/Development/comparisons01.py");
}
catch(Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
}
}
}
PYTHON CODE: (comparisons01.py)
===============
print("Enter two integers and I will tell you")
print("the relations they satisfy")
number1 = input( "Please enter the first integer: " )
number1 = int(number1)
number2 = input( "Please enter the second integer:" )
number2 = int(number2)
if number1 == number2:
print ("%d is equal to %d" % (number1, number2))
if number1 != number2:
print ("%d is not equal to %d" % (number1, number2))
if number1 < number2:
print ("%d is less than %d" % (number1, number2))
if number1 > number2:
print ("%d is greater than %d" % (number1, number2))
if number1 <= number2:
print ("%d is less than or equal to %d" % (number1, number2))
if number1 >= number2:
print ("%d is greater than or equal to %d" % ( number1, number2 ))
dummy=input()
我发现该解决方案实际上由于代码和 jar 兼容性而无法正常工作。
我在 Python 2.x 标准上重新编写了代码,安装了 Python 2.7 并在我的构建路径中使用了 jython-standalone 2.7 jar。它工作正常。你可以试试这段代码:
import random
import time
##Declare Variables
user_num=0
##lottery_num=random.randint(10,99)
lottery_num=12
##Input
print("Welcome to the Lottery Program!")
user_num=int(input("Please enter a two digit number: "))
print("Calculating Results.")
for i in range(3):
time.sleep(1)
print(".")
##Calc & Output
lottery_tens = lottery_num // 10
lottery_ones = lottery_num % 10
user_tens = user_num // 10
user_ones = user_num % 10
if lottery_num == user_num:
print("All your numbers match in exact order! Your reward is $10,000!\n")
elif lottery_tens == user_ones and lottery_ones == user_tens:
print("All your numbers match! Your reward is $3,000!\n")
elif lottery_tens == user_tens or lottery_ones == user_ones \
or lottery_ones == user_tens or lottery_tens == user_ones:
print("One of your numbers match the lottery. Your reward is $1,000!\n")
else:
print("Your numbers don't match! Sorry!")