On 02/12/2012 08:25 AM, William Stewart wrote:
I am trying to get 2 string variables and 2 integer variables to be able to be 
multiplied
can anyone tell me what I did wrong
str1 = raw_input("Type in a String: ")
str2 = raw_input("Type in a String: ")
int1 = raw_input("Type in a integer variable: ")
int2 = raw_input("Type in a integer variable: ")
print str1 + str2 + int1 + int2
import math
print str1, "*", str2, "*", int1, "*"int2  "=", str1, * str2, * int1 * int 2
and it wont let me write int2
I know this may look stupid to most people  but I am just new at this so dont 
laugh  :)


That's who this list is targeted at, people who are just learning Python. Are you new to programming, or just to Python? Anyway, welcome to Python-Tutor list.

If these 7 lines are in a file, and you try to run them, you get a specific error, pointing to a specific line. When asking questions about Python error messages, please post the actual message, as it generally contains lots of clues as to what's wrong.

davea@think:~/temppython$ python william.py
  File "william.py", line 7
print str1, "*", str2, "*", int1, "*"int2 "=", str1, * str2, * int1 * int 2
                                            ^
SyntaxError: invalid syntax

So now we both know the error is a syntax error, and it occurs on line 7, which is conveniently redisplayed with a caret pointing at where the error was discovered (notice that in many email systems a proportional font may hide the correct column. So trust what you saw on your own terminal window). Sometimes the actual syntax error is a few characters earlier, but this is as fine-tuned as most compilers can manage.

That print line has 4 errors that I can immediately spot, and the compiler told you about the first one. That error was in omitting the comma before the first occurrence of the variable int2. You butted a string literal right up to a variable name, with no operator between.

My usual advice when seeing a beginner with a complex line that gives a syntax error is to see if you can replace it by a few simpler lines. Then the error might be more obvious. So use several print lines. So what if the output isn't quite right; you have some more debugging to do before you'll even see that output.


Now let me ask you, how is str1 any different from int1 ? Python does not have typed variables, and it certainly pays no attention to the spelling of a name to guess what it's intended to hold. The same name str1 can hold a string one time, an integer another time, a list yet another. The only way you're going to get those 3rd and 4th lines to make integer objects is to convert the string that raw_input() returns into an integer. So use
     int1 = int(raw_input("xxxxx"))

The next problem will probably be easier for you to spot, but if not, remember to post the full error message, not some summary of it.





--

DaveA

_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to