[Tutor] New to Python
Hello, I am new to Python. I mean completely new and we're working on this problem set in class where they give us specs and we have to build something based off these specs. I have no idea what they're asking. Could someone help get me started on the path to figuring this out? Below is the question: 1. Write a program module with at least two functions. Follow this specifi- cation exactly for these two functions: 1. (a) One function, CalculateCentimeters, receives a value in inches and returns the equivalent value in centimeters. centimeters =2.54×inches 2. (b) The other function, CalculateInches receives a value in centime- ters and returns the equivalent value in inches. inches =centimeters/2.54 ... but you don’t 2.54 in your code 2 times. It’s a good candidate to be a module-level constant. Specified instructions about the internals of code, i.e., the names of your functions and how they behave, is called theinternal specification. It tells you, the author of the function, as well as programmers who call your function, how to call it and what to expect when it is called. You must following them exactly or call a meeting of your programming team because your choices here affect the others. For this exercise, you design the rest of the functions for your program, but be careful to keep all the code in functions. Invent and arrange functions as you wish to ask the user for: (a) a value (b) a unit of measure and call the appropriate function to print out the value in the other unit of measure. Specified instructions about the user’s view of your code (like just given) is called the external specification. Often the paying customer gives these directions so you must follow them exactly, doing things in the order given; but in this case, the internal design is up to you. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python
On 26/04/2014 23:53, jordan smallwood wrote: Hello, I am new to Python. I mean completely new and we're working on this problem set in class where they give us specs and we have to build something based off these specs. I have no idea what they're asking. Could someone help get me started on the path to figuring this out? Below is the question: 1. Write a program module with at least two functions. Follow this specifi- cation exactly for these two functions: 1. (a) One function, CalculateCentimeters, receives a value in inches and returns the equivalent value in centimeters. centimeters =2.54×inches 2. (b) The other function, CalculateInches receives a value in centime- ters and returns the equivalent value in inches. inches =centimeters/2.54 ... but you don’t 2.54 in your code 2 times. It’s a good candidate to be a module-level constant. Specified instructions about the internals of code, i.e., the names of your functions and how they behave, is called theinternal specification. It tells you, the author of the function, as well as programmers who call your function, how to call it and what to expect when it is called. You must following them exactly or call a meeting of your programming team because your choices here affect the others. For this exercise, you design the rest of the functions for your program, but be careful to keep all the code in functions. Invent and arrange functions as you wish to ask the user for: (a) a value (b) a unit of measure and call the appropriate function to print out the value in the other unit of measure. Specified instructions about the user’s view of your code (like just given) is called the external specification. Often the paying customer gives these directions so you must follow them exactly, doing things in the order given; but in this case, the internal design is up to you. What did you not understand about the answers you received to your original question "Help with an assignment"? Or if you could not be bothered to reply to either Alan Gauld or Dave Angel, why should we now waste our time attempting to help you? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python
> 1. Write a program module with at least two functions. Hint: "def" is the Python keyword used to define a function. You can read all about def in the docs on python.org. Alan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] converting strings to float: strange case
Hi, I'm trying to convert a string to a float. It seems a basic thing but I don't know why I'm getting this erroris Traceback (most recent call last): File "phresREADER.py", line 27, in tra = float(stri) ValueError: could not convert string to float: My file has this line 5.5e+000 5.5e+001 5.5e+002 5.5e+003 my code is: my_line = f.readline() avg_energySTR = [str(i) for i in my_line.split(' ')] for stri in avg_energySTR: tra = float(stri) do you have any idea? thanks Gabriele ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] converting strings to float: strange case
solved, sorry for the disturb http://stackoverflow.com/questions/23344345/strings-not-converting-to-float-as-expected/23344830#23344830 bye Gabriele 2014-04-28 11:13 GMT-04:00 Gabriele Brambilla < gb.gabrielebrambi...@gmail.com>: > Hi, > > I'm trying to convert a string to a float. It seems a basic thing but I > don't know why I'm getting this erroris > > Traceback (most recent call last): > File "phresREADER.py", line 27, in > tra = float(stri) > ValueError: could not convert string to float: > > My file has this line > > 5.5e+000 5.5e+001 5.5e+002 5.5e+003 > > my code is: > > my_line = f.readline() > avg_energySTR = [str(i) for i in my_line.split(' ')] > for stri in avg_energySTR: > tra = float(stri) > > do you have any idea? > > thanks > > Gabriele > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] converting strings to float: strange case
On Apr 28, 2014 11:14 AM, "Gabriele Brambilla" < gb.gabrielebrambi...@gmail.com> wrote: > > Hi, > > I'm trying to convert a string to a float. It seems a basic thing but I don't know why I'm getting this erroris > > Traceback (most recent call last): > File "phresREADER.py", line 27, in > tra = float(stri) > ValueError: could not convert string to float: > > My file has this line > > 5.5e+000 5.5e+001 5.5e+002 5.5e+003 > > my code is: > > my_line = f.readline() > avg_energySTR = [str(i) for i in my_line.split(' ')] > for stri in avg_energySTR: > tra = float(stri) Works for me. Try printing art in the loop > > do you have any idea? > > thanks > > Gabriele > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] converting strings to float: strange case
.split() will split things based on whitespace or newlines. Do you know that your file is only going to contain things that should convert to floats? If you post your entire code, the error you have included will be more helpful as it points to a certain line. The last line in your code has (stri) should be (str). On Mon, Apr 28, 2014 at 11:13 AM, Gabriele Brambilla < gb.gabrielebrambi...@gmail.com> wrote: > Hi, > > I'm trying to convert a string to a float. It seems a basic thing but I > don't know why I'm getting this erroris > > Traceback (most recent call last): > File "phresREADER.py", line 27, in > tra = float(stri) > ValueError: could not convert string to float: > > My file has this line > > 5.5e+000 5.5e+001 5.5e+002 5.5e+003 > > my code is: > > my_line = f.readline() > avg_energySTR = [str(i) for i in my_line.split(' ')] > for stri in avg_energySTR: > tra = float(stri) > > do you have any idea? > > thanks > > Gabriele > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected
Dear Python Tutor: Well,I am Stephen Mik,and I'm a beginning,rookie programmer who is just trying to get a class Assignment going. The instructor of my class does not accept email and she is not on Campus on Monday. So,my only recourse is to turn to Python Tutor for assistance. My program, Assignment4,does run partially. You can see the results of the Python Shell attached to this email. I also have included part of my code for your perusal. I must be doing something very wrong. The program is supposed to run a main loop ,for control of the program. The program DOES print out the prompts before the While Loop, but when it comes to a variable named"smv_guessNumber" the program DOES NOT prompt for the input for "smv_guessNumber" as it should. It is a mystery to me as to why the program will not get to the "smv_guessNumber=int(input("Think out a first guess:")". I am mystified why it doesn't reach that point in the program! Can anyone please help? I have attached the Python Traceback Error Output,which shows that at least part of the program IS working. I also have attached part of the code for the Assignment 4 which should help in the debugging.I need help ASAP,another program is due very soon and I have not even worked out the pseudocode for it yet! CONCERNED,Stephen W. Mik #CS 110A Spring 2014 Sect 4988 Assignment 4 #MODIFIED GUESS MY NUMBER PROGRAM #Date April ,2014 #Programmer Stephen W. Mik #This program is modified after a version found #in the TextBook "Python Programming 3rd Edition" #by Michael Dawson Copyright 2010;ISBN-13: 978-1-4354-5500-9 #Or the Alternate ISBN-10: 1-4354-5500-2. #It is based on the material found on page 50,and pages 81-84 #of the Book, along with a downloaded basic program Structyre #"guess_my_number" ,found in the book companion's Website Files #"www.courseptr.com/downloads" Chapter 3. ##EXPLANATION OF GUESS MY NUMBER### #This program uses a random number generator(supplied by Python 3.4.0) to #to pick a changeable number between 1 and 60. The User is prompted to guess the number #by inputting a guess at the prompt. The user is then advised whether the correct number #has been guessed. If it has been guessed correctly,the user is congratulated. If the number #has not been guessed correctly,the attempt is noted and quantified. then the user is told whether # the guess number was too large (and then for the user to guess a lower number) or the number #guessed was too small (and in this case for the user to guess a higher number). The number of #attempted non-successful guesses is accumulated and totalled and then when finally the User guesses #the correct number ; the guess attempts are outputted along with Congatulations. The User is queried #if they want to play the game again,if not,the Program ends. ## MAIN PROGRAM SECTION #Use the random import module for a random number between 1 and 60 import random print("\tWelcome to the Guess My Number Game! ") print("\n The Mysterious Number to guess is between 1 and 60. ") print("Try to guess the Mystery Number in as few tries as you can.\n") #Enter Main Query Loop print("Do you want to play the game?\n") print("\n") smv_grandCounter=int(input("Enter a 1 to play or 0 to exit: ")) while(smv_grandCounter=="1"): #Enter the play game main loop area smv_pickNumber=random.randint(1,60) #Create by the computer by random number generator a mystery number #Load random number into smv_pickNumber for storage #Initialize the variable smv_attemptCounter to 1 for 1 initial pass through the inner boundary values loop smv_attemptCounter=1 #Get first pick for Number Identification Loop #Initialize smv_guessNumber so Number Identification Loop can proceed with Conditional print("Think out a first guess between 1 and 60:") smv_guessNumber= int(input("Take a guess!")) #Enter Number Identification Loop while(smv_guessNumber!=smv_pickNumber): if (smv_guessNumber>smv_pickNumber): print("Guess of mystery number Too high,enter a lesser number: \n") smv_attemptCounter+=1 elif (smv_guessNumber < smv_pickNumber): print("Guess of mystery number Too Low.Enter a greater number \n") smv_attemptCounter+=1 elif (smv_guessNumber == smv_pickNumber): #Print Congratulatory Message,the mystery number,the number of attempts print("Congratulations! You have guessed the mystery number") print("You have used",smv_attemptCounter,"sample picks.\n") print("The mystery number was: ",pick_Number) print("You have utilized",smv_attemptCounter,"attempts") else: print("You have entered a decimal number,a negative number or some other Alphanumeric character") print("Redo the loop with the correct values") #Continue the loop,increment the number of attempts smv_attemptCounter+=1 print("Ent
Re: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected
That is definitely more useful information in answering your questions. Whenever you see the error you are getting: NameError: name 'smv_guessNumber' is not defined That means you are using a variable, in this case 'smv_guessNumber', that has not been created yet. The reason this is happening here is you need to import sys. On Mon, Apr 28, 2014 at 1:32 PM, Stephen Mik < mik.step...@yahoo.com.dmarc.invalid> wrote: > Dear Python Tutor: > Well,I am Stephen Mik,and I'm a beginning,rookie programmer who is > just trying to get a class Assignment going. The instructor of my class > does not accept email and she is not on Campus on Monday. So,my only > recourse is to turn to Python Tutor for assistance. > > My program, Assignment4,does run partially. You can see the results of > the Python Shell attached to this email. I also have included part of my > code for your perusal. > > I must be doing something very wrong. The program is supposed to run a > main loop ,for control of the program. The program DOES print out the > prompts before the While Loop, but when it comes to a variable > named"smv_guessNumber" the program DOES NOT prompt for the input for > "smv_guessNumber" as it should. It is a mystery to me as to why the program > will not get to the "smv_guessNumber=int(input("Think out a first > guess:")". I am mystified why it doesn't reach that point in the program! > Can anyone please help? I have attached the Python Traceback Error > Output,which shows that at least part of the program IS working. I also > have attached part of the code for the Assignment 4 which should help in > the debugging.I need help ASAP,another program is due very soon and I have > not even worked out the pseudocode for it yet! > CONCERNED,Stephen W. Mik > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected
I should probably clarify that this list is mainly for python2.7, correct me if I am wrong. On Mon, Apr 28, 2014 at 1:49 PM, C Smith wrote: > That is definitely more useful information in answering your questions. > Whenever you see the error you are getting: > > NameError: name 'smv_guessNumber' is not defined > > That means you are using a variable, in this case 'smv_guessNumber', that > has not been created yet. > > The reason this is happening here is you need to import sys. > > > On Mon, Apr 28, 2014 at 1:32 PM, Stephen Mik < > mik.step...@yahoo.com.dmarc.invalid> wrote: > >> Dear Python Tutor: >> Well,I am Stephen Mik,and I'm a beginning,rookie programmer who is >> just trying to get a class Assignment going. The instructor of my class >> does not accept email and she is not on Campus on Monday. So,my only >> recourse is to turn to Python Tutor for assistance. >> >> My program, Assignment4,does run partially. You can see the results >> of the Python Shell attached to this email. I also have included part of my >> code for your perusal. >> >> I must be doing something very wrong. The program is supposed to run >> a main loop ,for control of the program. The program DOES print out the >> prompts before the While Loop, but when it comes to a variable >> named"smv_guessNumber" the program DOES NOT prompt for the input for >> "smv_guessNumber" as it should. It is a mystery to me as to why the program >> will not get to the "smv_guessNumber=int(input("Think out a first >> guess:")". I am mystified why it doesn't reach that point in the program! >> Can anyone please help? I have attached the Python Traceback Error >> Output,which shows that at least part of the program IS working. I also >> have attached part of the code for the Assignment 4 which should help in >> the debugging.I need help ASAP,another program is due very soon and I have >> not even worked out the pseudocode for it yet! >> CONCERNED,Stephen W. Mik >> >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> >> > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected
Stephen Mik wrote: > My program, Assignment4,does run partially. You can see the results of the > Python Shell attached to this email. I also have included part of my code > for your perusal. > > I must be doing something very wrong. The program is supposed to run a > main loop ,for control of the program. The program DOES print out the > prompts before the While Loop, but when it comes to a variable > named"smv_guessNumber" the program DOES NOT prompt for the input for > "smv_guessNumber" as it should. It is a mystery to me as to why the > program will not get to the "smv_guessNumber=int(input("Think out a first > guess:")". > smv_grandCounter=int(input("Enter a 1 to play or 0 to exit: ")) > > while(smv_grandCounter=="1"): Hint: >>> 1 == "1" False ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected
The reason this is happening here is you need to import sys. > I don't know why you would think importing sys would fix this. docs say it accepts from sys.stdin On Mon, Apr 28, 2014 at 1:55 PM, Philip Dexter wrote: > > > On Mon, 28 Apr 2014, C Smith wrote: > > I should probably clarify that this list is mainly for python2.7, correct >> me if I am wrong. >> > > I don't think that is true. > > > On Mon, Apr 28, 2014 at 1:49 PM, C Smith >> wrote: >> > > > > The reason this is happening here is you need to import sys. >> > > I don't know why you would think importing sys would fix this. > > On Mon, Apr 28, 2014 at 1:32 PM, Stephen Mik > >> wrote: >> I must be doing something very wrong. The program is supposed to run >> a main loop ,for control of the program. The program DOES print out the >> prompts >> before the While Loop, but when it comes to a variable >> named"smv_guessNumber" the program DOES NOT prompt for the input for >> "smv_guessNumber" as it should. It >> is a mystery to me as to why the program will not get to the >> "smv_guessNumber=int(input("Think out a first guess:")". I am mystified >> why it doesn't reach that >> point in the program! Can anyone please help? I have attached the Python >> Traceback Error Output,which shows that at least part of the program IS >> working. I >> also have attached part of the code for the Assignment 4 which should >> help in the debugging.I need help ASAP,another program is due very soon and >> I have not >> even worked out the pseudocode for it yet! >> CONCERNED,Stephen W. Mik >> > > Your first while loop is not running. You convert smv_grandCounter to > an int but compare it with a string. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected
> > The reason this is happening here is you need to import sys. > I don't know why you would think importing sys would fix this. docs say it accepts from sys.stdin I now see that it is not necessary to import sys, although I am not sure why. On Mon, Apr 28, 2014 at 1:59 PM, C Smith wrote: > > > The reason this is happening here is you need to import sys. >> > > I don't know why you would think importing sys would fix this. > > docs say it accepts from sys.stdin > > > On Mon, Apr 28, 2014 at 1:55 PM, Philip Dexter wrote: > >> >> >> On Mon, 28 Apr 2014, C Smith wrote: >> >> I should probably clarify that this list is mainly for python2.7, >>> correct me if I am wrong. >>> >> >> I don't think that is true. >> >> >> On Mon, Apr 28, 2014 at 1:49 PM, C Smith >>> wrote: >>> >> >> >> >> The reason this is happening here is you need to import sys. >>> >> >> I don't know why you would think importing sys would fix this. >> >> On Mon, Apr 28, 2014 at 1:32 PM, Stephen Mik >>> wrote: >>> I must be doing something very wrong. The program is supposed to run >>> a main loop ,for control of the program. The program DOES print out the >>> prompts >>> before the While Loop, but when it comes to a variable >>> named"smv_guessNumber" the program DOES NOT prompt for the input for >>> "smv_guessNumber" as it should. It >>> is a mystery to me as to why the program will not get to the >>> "smv_guessNumber=int(input("Think out a first guess:")". I am mystified >>> why it doesn't reach that >>> point in the program! Can anyone please help? I have attached the Python >>> Traceback Error Output,which shows that at least part of the program IS >>> working. I >>> also have attached part of the code for the Assignment 4 which should >>> help in the debugging.I need help ASAP,another program is due very soon and >>> I have not >>> even worked out the pseudocode for it yet! >>> CONCERNED,Stephen W. Mik >>> >> >> Your first while loop is not running. You convert smv_grandCounter to >> an int but compare it with a string. > > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected
On Mon, Apr 28, 2014 at 10:32:06AM -0700, Stephen Mik wrote: > I must be doing something very wrong. The program is supposed to > run a main loop ,for control of the program. The program DOES print > out the prompts before the While Loop, but when it comes to a variable > named"smv_guessNumber" the program DOES NOT prompt for the input for > "smv_guessNumber" as it should. It is a mystery to me as to why the > program will not get to the "smv_guessNumber=int(input("Think out a > first guess:")". I am mystified why it doesn't reach that point in the > program! That was a tricky one! It took me a while to see it, but the problem comes from three factors. Firstly, these two lines: smv_grandCounter=int(input("Enter a 1 to play or 0 to exit: ")) while(smv_grandCounter=="1"): In the first line, you get input from the user, either "1" or "0". User input is a string, then you convert to an int. But in the second line, you compare it to the string "1". So regardless of whether the user types "1" or "0", the while loop is ALWAYS skipped: int 1 == "1" returns False int 0 == "1" returns False and the while loop never runs at all. I recommend you either remove the call to int(), and keep smv_grandCounter as a string, or you change the while condition to while smv_grandCounter == 1: (But don't do both!) Secondly, since the while loop is skipped, the line which initialises the smv_guessNumber variable: smv_guessNumber= int(input("Take a guess!")) also gets skipped. So smv_guessNumber doesn't get a value. Thirdly, you haven't indented the "Number Identification Loop". The game logic *should* be: # Game loop while you want to play a game: set up the next game # Number Identification Loop while your guess is not equal to the number: ... The number identification loop only happens while you want to play a game. But you have it like this by mistake: # Game loop while you want to play a game: set up the next game # At this point, the game loop has finished, # so you no longer wish to play # Now you start the Number Identification Loop while your guess is not equal to the number: ... You need to take this line: while(smv_guessNumber!=smv_pickNumber): and all the code which belongs to it, and indent it one extra block, so it is considered *inside* the "do you want to play a game?" loop. Once you have fixed those issues, you can then continue your testing. By the way, what is the meaning of the mysterious "smv_" prefixes on all your variables? Please don't tell me that stands for "Stephen Mik Variable." It seems to me that *every* variable has the same smv_ prefix, and so that prefix doesn't have any meaning. It would be like me deciding to add "blah" to the beginning of every word: blahit blahwould blahbe blahlike blahme blahdeciding blahto blahadd "blahblah" blahto blahthe blahbeginning blahof blahevery blahword The "blah"s are just meaningless noise. When programming, your code should all carry its weight. Programming is hard enough without sticking "blah" at the beginning of every word! Variable names should describe what the variable stands for, or at least follow some common convention like "x" for mathematical quantities. If "smv_" doesn't carry it's weight in helping your code be more easily understood, you should remove it. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected
On Mon, Apr 28, 2014 at 01:50:56PM -0400, C Smith wrote: > I should probably clarify that this list is mainly for python2.7, correct > me if I am wrong. Nope, any version of Python. If anyone is silly enough to be using Python 0.9 (which is over 20 years old!) I can try to answer their questions. The answer will nearly always be "don't use that version, it is ancient!!!" but still it will be an answer :-) More seriously, anything from Python 2.4 to 3.4 is still under commercial or free support. Many questions, especially beginner questions, don't usually depend on the version, also sometimes they do. Most programming errors are errors regardless of the version: x = 1 + "2" # is an error in every version of Python -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Stephen Mik-Almost Brand New to Python 3.4.0-"Guess My Number" program is syntactically correct but will not run as expected
On Mon, Apr 28, 2014 at 01:49:31PM -0400, C Smith wrote: > That is definitely more useful information in answering your questions. > Whenever you see the error you are getting: > > NameError: name 'smv_guessNumber' is not defined > > That means you are using a variable, in this case 'smv_guessNumber', that > has not been created yet. So far so good. But your next comment: > The reason this is happening here is you need to import sys. Not so much. This has nothing to do with importing sys. If it were, the error would likely have been: NameError: name 'sys' is not defined -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python
Hi Jordan, You probably want to read up to chapter 3 (including the "Functions" chapter) in "How to Think Like a Computer Scientist": http://www.greenteapress.com/thinkpython/html/index.html or some equivalent tutorial, so that you at least know what the terms in the problem statement means. In particular, you'll want a very concrete idea of what a _function_ is, because this assignment is all how to write and use functions. That's the core takeaway from the problem, so learn about functions. Are you using a particular book or material in your learning? If so, mention that. Maybe one of us here has also read the same book and can point out things for you to look at. Also are there particular terms in the problem statement that are confusing? If so, point them out, and one of us here on Tutor can probably help. Personally, I would actually treat this problem in at least two steps. You don't have to get your whole program perfect the first time. The first paragraph which says "Write a program module..." up to the description talking about CalculateInches(). But I would not initially follow any of the paragraph material after "... but you don't...". Get the two functions working first. Learn how to test and run those functions first. If you have questions on how to do so, ask. Ignore the advice about module level constants _until_ you've got the functions working ok. You'll be a better position to improve your solution to fit the final approach. Good luck to you! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Keeping change-in-place vs. copy methods straight
I can't claim to be new to programming, but I've dabbled in Python over and over again to get small problems and puzzles resolved. One thing that I find I can't keep straight are the methods that change a list in place, vs. those that return a copy (sometimes transformed) of the list. Call me old-fashioned, but my programming experience mostly comes from languages where you assigned the output of a function to another variable, so you always had a copy of whatever you were working on. var array; sorted = array.sort(); If you didn't care to keep both copies, you could always re-assign the returned value to the original variable. array = array.sort(); If I try to do the same in Python: sorted = arrayList.sort() sorted comes back as None, while arrayList has changed its order. Is there some sort of rule-of-thumb to determine if a function is in-place or returns a value? Antonio Rodriguez ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to Python
On Sat, Apr 26, 2014 at 03:53:33PM -0700, jordan smallwood wrote: > Hello, > > I am new to Python. I mean completely new and we're working on this > problem set in class where they give us specs and we have to build > something based off these specs. I have no idea what they're asking. > Could someone help get me started on the path to figuring this out? Yes. You need to write two functions, one to convert from inches to centimetres and one from centimetres to inches. You've learned about functions in maths class, I expect. This is similar. Here's how you write functions in Python. def double(x): """Return double x.""" return 2*x def half_plus_one(x): """Return half of x, plus 1.""" return x/2.0 + 1 The keyword "def" starts the definition of the function. It is followed by the name of the function, then inside round brackets (parentheses) is a list of the arguments that the function requires. In my examples, the function only takes one argument, x, which you can assume is a number. If possible, you should use a more descriptive name than "x". The next line, a string starting and ending with THREE quotation marks, is called a "doc string". It's just a short comment explaining what the function does. (It's also optional, but recommended.) Inside the function, all your code needs to be indented by one level. You should indent by either: Four spaces (this is recommended) One Tab (if you must) although any number of spaces is allowed, so long as it is consistent. Whatever you use, pick one, and use it for all indentation. Python will complain, or worse, do the wrong thing, if you have inconsistent indentation. (Say, four spaces on one line, then three on the next.) Both my functions are simple enough that I only have a single line. The keyword "return" tells Python what value should be returned. It should, I hope, be obvious that 2*x gives two times x. Is that enough to get you started? -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Keeping change-in-place vs. copy methods straight
Hi Antonio, Unfortunately, I don't think it's apparent whether or not a function applies mutations or is a pure computation. In Python, those are by convention or documentation rather than part of the language. There are other programming languages can control the effects and scope of mutation, but Python is not one of those languages. In particular, I know from experience that list.sort() mutates the list, and so it doesn't return a useful return value. On the other hand, there's a separate built-in function called "sorted()" that can sort lists, and it does not mutate the original list. ### >>> lst = [3, 1, 4, 1, 5, 9, 2, 6] >>> lst2 = sorted(lst) >>> lst [3, 1, 4, 1, 5, 9, 2, 6] >>> lst2 [1, 1, 2, 3, 4, 5, 6, 9] ### See: https://docs.python.org/2/library/functions.html#sorted ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Keeping change-in-place vs. copy methods straight
On 4/28/2014 11:45 AM, taserian wrote: Is there some sort of rule-of-thumb to determine if a function is in-place or returns a value? my rule of thumb is to ask: Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> help([].sort) Help on built-in function sort: sort(...) L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) -> -1, 0, 1 >>> help([].reverse) Help on built-in function reverse: reverse(...) L.reverse() -- reverse *IN PLACE* >>> The *IN PLACE* tells me. HTH, Emile ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor