Quote:
Originally Posted by Acemanhattan
Brand new to programming, just started going through the MIT opencourse and have some questions regarding Python.
If I open "Python Shell" and from there open "New Window" is this where I will be writing my programs? I then save it there and use the F5 function to run it in the shell correct?
Why when I am using that "New Window" does it automatically indent sentences that I don't think it should? For example I type the following for the first line:
outstanding_balance = int(raw_input("What is your outstanding balance?")
When I press enter it indents me about 7 tab spaces on the next line like:
outstanding_balance = int(raw_input("What is your outstanding balance?")
print "What is your outstanding balance?"
also if I'm not mistaken my first command is saying that whatever the user inputs when prompted, it will be turned from a string into an integer correct? And the second command should print the prompt for the user, correct?
When I try to run that code I get a syntax error.
Edit to add: 2+2 isn't displaying the indent the way I'm intending to show it, imagine "print" starting directly under "raw_input"
|
Ok, for starters I'm not quite sure what you mean by "Python shell" and "New Window", but I can answer your other questions.
As for this line:
Code:
outstanding_balance = int(raw_input("What is your outstanding balance?")
The indent is happening because your parenthesis aren't balanced. You need one ")" to close the "raw_input(" function, and another ")" to close the "int(" function.
Quote:
also if I'm not mistaken my first command is saying that whatever the user inputs when prompted, it will be turned from a string into an integer correct? And the second command should print the prompt for the user, correct?
When I try to run that code I get a syntax error.
|
Yes you are correct. The int() function will convert an object (in this case a String) into an integer. Hint: Type "help(int())" in your python shell.
The reason you are getting an error could be for a few reasons. As I said before you need to close your open parenthesis. In addition, the int() function will only work on a properly formatted String.
Try the following:
int('1')
int('3.4')
int('two')