[Tutor] string integers?

William Stewart williamjstewart at rogers.com
Mon Feb 13 00:34:28 CET 2012


this is the code I have
 
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
int1 = int(raw_input(""))
print str1,
print str2,
print int1, "*", int2
print "="

and it does not give me an error message when I run it, the program runs fine except its did not multiply the 2 integer variables i entered
 
it looks like this
 
Type in a String: hello
Type in a String: hi
Type in a integer variable: 4
Type in a integer variable: 5
hellohi45
 
This part is exactly what I want it to look like
except I want it to multply the 2 numbers I inputed (4 & 5 in this example)
 
thanks again for your time and sorry if I seem rude I just dont have alot of time these days
I know you all dont have alot of time either and thats why I am extremely appreciative of everyones help
 

 


--- On Sun, 2/12/12, Dave Angel <d at davea.name> wrote:


From: Dave Angel <d at davea.name>
Subject: Re: [Tutor] string integers?
To: "William Stewart" <williamjstewart at rogers.com>
Date: Sunday, February 12, 2012, 5:07 PM


On 02/12/2012 02:41 PM, William Stewart wrote:
> hello thank you for your reply

That was a post to the list;  you replied privately,  but I'm going to 
just elaborate my earlier points.  Try again, but use reply-all to one 
of the messages in the thread.

You've got a new version of stuff, but no indication what the code now 
looks like, or what is "not working".

Be explicit when requesting help.  I and others pointed out a few things 
wrong;  there are others. So don't make us guess what state you're in. 
Hopefully you're here to learn, and that happens best when you make 
clear questions, and get good responses.

Show your code, show the error,and use cut&paste for the error you get.
"It's not working" is not an error message.

You either get an error message:  quote the entire traceback
Or it doesn't do what you expect:   tell what you expected, and show 
what you got instead.


> I fixed the error the only problem now is how do i get the 2 spereate integers to multiply? but I still need the 2 strings to print
>
> I tried
>
> print str1,
> print str2,
> print int1, "*", int2
> print "="
>
> I think I am missing something
> I know the * is to multiply but its not working
> thank you
>
>
>
> --- On Sun, 2/12/12, Dave Angel<d at davea.name>  wrote:
>
>
> From: Dave Angel<d at davea.name>
> Subject: Re: [Tutor] string integers?
> To: "William Stewart"<williamjstewart at rogers.com>
> Cc: tutor at python.org
> Date: Sunday, February 12, 2012, 9:51 AM
>
>
> 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 at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120212/581d78ec/attachment-0001.html>


More information about the Tutor mailing list