1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
print("This program translates user input (in \"python\") to Java!\n")
# Java List
Java_list = ["if (condition) {Statement}","else {Statement}","while (condition) {Loop body}","for (arb_var) {Loop body}",
"var variableName = value","System.out.printIn","function functionName(parameter)","// line comment, /* paragraph comment */, /** javadoc comment */"]
# Python List
Py_list = ["if (condition):","else:","while (condition):","for (arbitrary_var) in (comparison):","variable_name = variable",
"print(statement)","def function(parameters):","#comments"]
# descriptions of the syntax
description = ["Checks if an expression is True or False.","If the if statement is False, the else branch runs.","Loop that iterates when condition is True",
"Iterates over any sequence, such as list or string.","A storage place for data.",
"Outputs a string.","A user-created function to perform a task.",
"Notes added to understand the code."]
def translator(py):
Python_index = 0
len_py = len(Py_list) - 1
new_py = ""
if " " in py: # checks if double space or more in user_input
if " " in py[0]:
new_py = py.strip() # takes out the extra space
return(translator(new_py))# uses it to run in function again
else:
import re
new_py = re.sub(' +',' ',py) # takes out spaces in the middle
return (translator(new_py)) # uses it to run in function again
else:
for term in Py_list:
if py in term:
return ("\nPython syntax given --> " + Py_list[Python_index] +
"\nUse of syntax --> " + description[Python_index] +
"\nJava equivalent --> " + Java_list[Python_index] + "\n")
break
else:
Python_index += 1
if Python_index > len_py:
return ("The syntax given cannot be translated at this moment.")
while True:
user_input = input("Enter a statement in Python to translate into Java (or 'quit' to break): ").lower()
print("---------------------------------------------")
if user_input == "quit" or user_input == 'q':
break
else:
print(translator(user_input))
print("Thank you for using our translator!")
Enter to Rename, Shift+Enter to Preview