On 13 February 2012 01:34, William Stewart <williamjstew...@rogers.com> wrote: > > 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) >
Hi William, That is a much better starting point from which to get help. It looks to me as though you took the responses concerning form that I and other gave you seriously; I'm glad. (I sympathize about the difficulty to find time to ask well. It does, however, take less time in the long run than asking several rounds of quick to compose questions.) Steven D'Aprano has given you enough that you should be able to make progress and ask again if needed. I did, however, want to point out that in my first message to you, when I suggested a function for you to run, it was with an eye to helping you to discover the problem. Here's the function and the results of running it in idle: IDLE 2.6.6 >>> def test(): data = raw_input("give me an integer") print type(data) print "a string" * "another" >>> test() give me an integer42 <type 'str'> Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> test() File "<pyshell#0>", line 4, in test print "a string" * "another" TypeError: can't multiply sequence by non-int of type 'str' >>> (Here, 42 is my input.) You can see that the type of data (the value returned by the raw_input call) is str---a string. The TypeError is telling you that the code I gave tries to multiply by a string and that caused a TypeError as multiplication isn't an operation defined for strings as the right-hand multiplier. Steven's email shows you how to surmount that problem; you must use int() to turn the returned value of raw_input into an integer. Compare: >>> def test2(): data = int(raw_input("give me an integer")) print type(data) print data * data >>> test2() give me an integer4 <type 'int'> 16 of course, there are still things that can go wrong: >>> test2() give me an integer3.1415 Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> test2() File "<pyshell#5>", line 2, in test2 data = int(raw_input("give me an integer")) ValueError: invalid literal for int() with base 10: '3.1415' >>> test2() give me an integerFourtyTwo Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> test2() File "<pyshell#5>", line 2, in test2 data = int(raw_input("give me an integer")) ValueError: invalid literal for int() with base 10: 'FourtyTwo' >>> In both cases, I entered some string that int() cannot turn into an integer. If you get the basic idea working for the case where the user enters sane values, we can talk about how to deal with such cases if need be. Best, Brian vdB _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor