Question:
User Input Help! Python Programming?
1970-01-01 00:00:00 UTC
User Input Help! Python Programming?
Five answers:
om
2009-02-13 00:10:22 UTC
When you launch IDLE you'll be in the interactive window, the one that gives the '>>>' prompt. In the interactive window each chunk of code gets executed as soon as you've finished typing it. That makes it the ideal place to try out little chunks of code one statement at a time but it's not the right place for creating larger multi-line programs that you want to be able to save and run repeatedly.



The place to work on larger programs is in an editing window. To do that, in the interactive '>>>' window select 'New Window' from the 'File' menu. That will bring up a fresh empty window. You can now type your program into the new window. The new window doesn't have a prompt, it looks just like editing a file -- because in that window you *are* just editing a file.



When you're happy with the program you've entered into the editing window you can hit F5 to run it and find out whether it works. Your program will actually be run inside the IDLE interactive window and that's where you'll be able to see and respond to the prompts issued by the program. That's also where you'll see any error messages caused by problems in the program. After your program has been executed you can go back into the editing window and make changes -- fix bugs or add to the program -- and then use F5 to run it again until it does exactly what you want.



Then (or at any other time) you can 'Save' the program by using the 'File' menu in the editing window. When you want to work on the program again you can launch IDLE and use 'File'->'Open' to read your program file into an editing window, make whatever changes you need to make, test the program and then 'Save' the modified version.



In this particular case you'll find that you don't get the right answer for the sum of the two numbers you enter. That's because raw_input() returns a text string, and when you tell Python to add two text strings together it simply concatenates the two strings into a longer string. (The string "32" plus the string "31" gives "3231".)



To get the answer you want you need to tell Python to convert the strings into integers and then tell it to add those integers together and print out the sum. When Python adds integers it does the arithmetic addition that you want here: the integer 32 plus the integer 31 gives 63.



You can express these string-to-integer conversions very concisely but if you're just starting to learn Python then it's more understandable if you do it explicitly step by step using additional variables, like this:



astring = raw_input("Enter a number: ")

a = int(astring)

bstring = raw_input("Enter another number: ")

b = int(bstring)

print a, "plus", b, "equals", c



If you're just starting to program then everything will be a bit strange for a while. Just try to stay calm; it's really not a hard thing to do if you break the problem down into pieces and tackle those pieces one at a time.
?
2016-05-25 02:30:00 UTC
To build on the other answer the preferred method to open a file is to use a "with" statement: # Example code # name = input("Enter your name") with open('log.txt', "a+") as f: f.write(name) # End example# The "a" above means that anything you write will go at the end of the file rather than overwriting it, and the "+" means that if the file doesn't exist it will create it for you. One less step for you to do manually.
Rick B
2009-02-12 23:07:12 UTC
Both scripts will run, but give different results--perhaps more on that later.



In idle you have a couple different windows. One says "Python Shell" at the top, has some info about firewalls and a prompt >>>. This is where the results of running a script will show up (including where you would type in information to satisfy a request by raw_input(). If you don't see this window don't worry it will show up when run a script.



The other window(s) are the scripts themselves. The window name is the name of the script (once you save it) These windows have the Run menu at the top (F5 is the shortcut to the item in Run). If you don't see this window select 'New Window' under File.



You should type in the above script (there's no shame in copy/paste either) and save it somewhere you can find it later with an meaningful and creative name. Now you can run it with F5 or select the menu item. Another option under Run is Check module. This will do a simple check of program structure (indents, semi-colons and such).



When you run the script the prompts will be displayed (one at a time) and the script will continue after you type in the input and press return or enter. The results will print and the shell prompt (>>>) will come back. This is "running the script". The other possible outcome is a lot of red text telling you about an error in the script. The line number is the line of the script with the error. Occasionally the real error is earlier in the script and the displayed error is the just a consequence of that earlier programming error. This is "debugging" which is not as fun as running the script.



Now for the bonus: raw_input will take the text you type in and place it in a and b in turn. When you add (a+b) you are actually adding two strings as in '1'+'2' = '12'. input will try to eval() the input so text that is all numeric typed in at the prompt will be converted to a number and you'll get 1+2 = 3. But if you type in any text the eval() will try to find a function, variable or expression that matches what you've typed and will give an error if it can't. Try 1 for the first input and a for the second.



BTW: 32 and 31 should be dropped from the arguments to input().



Hopefully everything will click. If not, try another question with details of what's not working or what doesn't make sense.



Additional Info:

I guess I'm just confused but if you ran either of the two scripts in your question you wouldn't just get 32. I'm not trying to make it difficult for you. I just don't know what's not working right. If my rambling about idle was unnecessary then perfect! Let's concentrate on the scripts: raw_input and input are different functions with different behavior. Let's use raw_input. Raw_input inputs string's so to fix the script you need to cast the string's to int's so that adding them gives the expected results. One way to do this would be to replace the line that adds with:



c = int(a) + int(b)



Now, if creating proof that the script works is the issue then all you need to do is save your script as, say, add2.py, close the python shell window if it's open and press F5 in the editor window where the add2.py text is. This will re-open the shell, prompt for your inputs, and print the results. You can then save this window as a text file named, say, add2_output.txt. Or you could print both. Please tell me what I'm missing, what's not working.
2009-02-12 22:54:26 UTC
By no means am I an expert in python, but try just declaring your variables as numbers i.e.

a = 32

b = 31



then try it over again. Also, if your coding software has a debug mode they you may be able to run it from there, or try compiling it and testing it.



What your doing is just adding 32 to the end of the input string making it say "Please select a number: 32" instead of saying "Please select a number:" when you run it the second way. This wont give a or b actual data , it will only change what it says when it asks for the number.
Fudge
2009-02-13 00:11:26 UTC
Don't use input() ,

just go with raw_input() but cast the input into a number using the float() , because raw_input() returns a string :



a = float( raw_ input("Please select a number:") )

b = float( raw_input("And another one:") )

c = a + b

print a, "plus", b, "equals", c


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...