MetalOperator wrote:Hi, all I'm new here, just started learning python 2 days ago. I have no experience in programming whatsoever.
Hello, you'll have to excuse me, I haven't used Python in a while, but:
You'll want to put your code into 'code' tags, firstly. Next, you're problem is pretty simple;
- Code: Select all
def fermat() :
prompt = raw_input('Enter 4 integers to test Fermats Theorem : ')
input = raw_input(prompt) # assings four integers to "input"
int(input) # convert arguments to integers
fermat(check_fermat)
def check_fermat(a, b, c, n) :
if n > 2 and a ** n + b ** n == c ** n:
print 'Holy shit, Fermat is wrong!'
else:
print 'No, that doesnt work.'
check_fermat(input)
The reason 'input' is making Python flip a bitch, is because 'input' is already a pre-defined function name. 'input' is the buffered version of 'raw_input'. Now, I'm not exactly sure why you're setting input equal to prompt, as it's essentially the same thing... Somewhat confusing. Anywho, you're use of int is wrong. In Python, strings are only set to integers before they are put to use. You can't say:
- Code: Select all
a = "Hi dad!"
b = "Moo"
int(a)
int (b)
print a + b + 2
Because 'a' and 'b' are only integers for those lines, after that they are converted back to strings. Instead, you convert them into integers when you're using them as integers. So, instead you would do this;
- Code: Select all
a = "Hi dad!"
b = "Moo"
print int(a) + int(b) + 2
Also, Python gets really confused, when you enter more than one argument in 'raw_input' and expect to use it all. Instead, it would prefer you to either parse your input, or use multiple lines, like such:
- Code: Select all
foo = raw_input("Enter your birthday! : ")
bar = raw_input("Enter your name! : ")
birthday = foo
name = bar
Next, you never assigned what a, b, c, and n, were. It's like somebody telling you that this is a new symbol, without telling you what it means, and expecting you to work with it. Also, you defined 'check_fermat' to have 4 arguments, yet you only supplied one 'input'.
- Code: Select all
check_fermat(input)
Finally, you never gave any arguments inside 'fermat' when you defined it, yet you call it _with_ an argument
- Code: Select all
fermat(check_fermat)
Now, there are several ways to solve your problem, by using multiple inputs, or parsing, or using sys.argv. I'll show you multiple inputs and sys.argv's, though you should still look up parsing.:
Multiple inputs:
- Code: Select all
prompt = raw_input('Enter 1 integers to test Fermats Theorem : ')
prompt1 = raw_input("Enter another integer: ")
prompt2 = raw_input("Enter a final integer: ")
prompt3 = raw_input("Enter a power you want to test: ")
def check_fermat() :
a = prompt
b = prompt1
c = prompt2
n = prompt3
if int(n) > 2 and int(a)**int(n) + int(b)**int(n) == int(c)**int(n):
print 'Holy shit, Fermat is wrong!'
else:
print 'No, that doesnt work.'
A system argument is when you supply arguments when you run it from the command line, for instance, if you run;
"python FermatProblem.py --help"
In the terminal, --help would be an system argument. Or, if you ran;
"python FermatProblem.py 1 2 3 4"
In the terminal, 1, 2, 3, and 4 would all be system arguments. To access these, in Python, you use 'sys.argv'. All the system arguments are stored in array, and are accessed like any other element in an array is. The first element (0) is always reserved for the file name, while the rest are supplied by user designated arguments. For instance;
- Code: Select all
import sys
a = sys.argv[1]
print a
If you ran "python SysArgDemonstration.py Help" in the terminal, it would output; "Help". This can be used for your fermat problem;
Sys.argv
- Code: Select all
import sys
a = sys.argv[1]
b = sys.argv[2]
c = sys.argv[3]
n = sys.argv[4]
if n > 2:
if int(a)**int(n) + int(b)**int(n) == int(c)**int(n):
print "Holy shit, Fermat is wrong!"
else:
print "Nope, he was right."
So when you run this from the terminal, you have to supply 4 arguments afterwards (all separated by spaces) for the corresponding numbers. If I run this from the terminal with the command; "python FermatProblem.py 1 2 3 4" it would produce the answer; "Nope, he was right.".
Hope that helped, and welcome to HTS!
~Cent