Using raw_input as a parameter within a function. PYTHON

Discuss how to write good code, break bad code, your current pet projects, or the best way to approach novel problems

Using raw_input as a parameter within a function. PYTHON

Post by MetalOperator on Mon Feb 13, 2012 2:18 am
([msg=64318]see Using raw_input as a parameter within a function. PYTHON[/msg])

Hi, all I'm new here, just started learning python 2 days ago. I have no experience in programming whatsoever.

I'm going to paint the picture that my question dervies from I promise it's a specific question...

I'm using a tutorial that I printed off from greentearpress.com/thinkpython/ I'm in chapter 5.
OS is Ubuntu 11.04, using the Emacs editor.

Exercise 1
Fermat's Last Theorem says that there are no integers a, b, and c such that for any vaulues of n greater than 2.

a ** 2 + b ** 2 == c ** 2.

1. Write a function named check_fermat that takes four parmaters--a, b, c, and n, and that checks to see if Fermat's theorem holds. If n is greater than 2 and it turns out to be true that

a ** n + b ** n == c **n

the program should print, "Holy crap, Fermat was wrong!" Otherwise the program should print, "No, that doesn't work."

So I wrote:

def check_fermat(a, b, c, n) :
if n > 2 and a ** 2 + b ** 2 == c ** 2
print 'Holy crap, Fermat was wrong!'

else:
print ' No that doesn't work.'

check_fermat(1, 2, 3, 4)


It works fine.

Exercise 2

Write a function that prompts the user to input values for a, b, c, + n, converts them to integers and uses check_fermat to check whether they violate Fermat's theorem.

I wrote:

#!/usr/bin/python

#Filename: fermat.py

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)


Thus my question is wtf am I doing wrong? How can I get the check_fermat function to accept "input" as an argument? The interpreter keeps saying that it needs 4 arguments but 1 is given. The tutorial doesn't explain how to use " raw_input" within a function parameter either.

Do I need to write the program with check_fermat being executed first? Then the fermat function?

I'm extremely lost and neither google nor the forums here have helped me thus far.

I apologize if this is the wrong part of the forum or this is a super newb question. Any help is greatly appreciated I've been trying to figure this out on my own for about 12 hours.

Note: I am using the correct indentations, this will not let me post it correctly. Sorry for any confusion
MetalOperator
New User
New User
 
Posts: 2
Joined: Mon Feb 13, 2012 1:55 am
Blog: View Blog (0)


Re: Using raw_input as a parameter within a function. PYTHON

Post by centip3de on Mon Feb 13, 2012 3:01 pm
([msg=64333]see Re: Using raw_input as a parameter within a function. PYTHON[/msg])

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
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1409
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


Re: Using raw_input as a parameter within a function. PYTHON

Post by MetalOperator on Tue Feb 14, 2012 1:35 am
([msg=64347]see Re: Using raw_input as a parameter within a function. PYTHON[/msg])

Yes that helped alot, I'm thinking I'd be much better off buying a book, as every tutorial I've come across is rather incomplete. For instance, this tutorial did not explain any of what you wrote prior to having me do this exercise. Or maybe... I'm just feeble minded. Thanks a ton for your response and time, I truly appreciate it!
MetalOperator
New User
New User
 
Posts: 2
Joined: Mon Feb 13, 2012 1:55 am
Blog: View Blog (0)


Re: Using raw_input as a parameter within a function. PYTHON

Post by cyberdrain on Tue Feb 14, 2012 5:57 am
([msg=64349]see Re: Using raw_input as a parameter within a function. PYTHON[/msg])

An easy to follow tutorial for Python is Learning Python The Hard Way. It actually teaches Python rather slowly (not hard at all), while giving you a feel what programming is like. It's the way I'm learning it. The only thing I didn't like was that the pace was sometimes so slow (I already did programming in other languages) that it made the exercises tedious. The best part is by typing all exercises in, you'll work on your typing skills and because of that and the constant repetition you'll learn way faster and won't forget as much. Everything that doesn't get explained right away will be in the next exercise and the author sort of drags you along for the ride. I highly recommend it!
Free your mind / Think clearly
User avatar
cyberdrain
Poster
Poster
 
Posts: 182
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: Using raw_input as a parameter within a function. PYTHON

Post by centip3de on Tue Feb 14, 2012 1:56 pm
([msg=64358]see Re: Using raw_input as a parameter within a function. PYTHON[/msg])

MetalOperator wrote:Yes that helped alot, I'm thinking I'd be much better off buying a book, as every tutorial I've come across is rather incomplete. For instance, this tutorial did not explain any of what you wrote prior to having me do this exercise. Or maybe... I'm just feeble minded. Thanks a ton for your response and time, I truly appreciate it!


Well, most tutorials end up being like that. I learned Python by trial and error, really; using somebody else's code and messing it up to see what I could do. I messed up each line, until I knew what that line meant, what it did, and continued onward. If you don't like seeing and reading through a ton of error messages, it may not be the best way to learn a new language. Though, a good book is always a good investment.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1409
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests