[Tutor] Input() is not working as expected in Python 3.1

Rich Lovely roadierich at googlemail.com
Mon Feb 15 16:45:08 CET 2010


On 15 February 2010 15:15, Yaraslau Shanhin <yaraslau.shanhin at gmail.com> wrote:
> Code:
> text = str(input("Type in some text: "))
> number = int(input("How many times should it be printed? "))
> print (text * number)
> Output:
> Type in some text: some
> How many times should it be printed? 5
> Traceback (most recent call last):
>   File "test4.py", line 2, in <module>
>     number = int(input("How many times should it be printed? "))
> ValueError: invalid literal for int() with base 10: 'How many times should
> it be printed? 5'
>
> That is exactly how it looks like. It is happening all the time when I run
> program in Komodo Edit, but in IDLE everything works fine (interpreter is
> the same):
>
> Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)]
> on win32
> Type "copyright", "credits" or "license()" for more information.
>>>> text = str(input("Type in some text: "))
> Type in some text: some
>>>> number = int(input("How many times should it be printed? "))
> How many times should it be printed? 5
>>>> print (text * number)
> somesomesomesomesome
>>>>
> For your suggestion
> Code:
> text = str(input("Type in some text: "))
> snumber = input("How many times should it be printed?")
> try:
>  number = int(snumber)
> except ValueError:
>  print(ssnumber, "is not an integer.")
> else:
>  print (text * snumber)
>
>
> Output:
> Type in some text: some
> How many times should it be printed?5
> How many times should it be printed?5 is not an integer.
> On Mon, Feb 15, 2010 at 4:06 PM, bob gailer <bgailer at gmail.com> wrote:
>>
>> Yaraslau Shanhin wrote:
>>
>> > Hello All,
>>
>> Hello.
>>
>> Suggestion for future questions - just include enough to identify the
>> problem. We don't need to know that this is from a tutorial or what the
>> exercise is. Also (at least I prefer) plain text without formatting or
>> color.
>>
>> Sufficient therefore is:
>>
>> -------------------------------------------
>> using  Python 3.1:
>> text = str(input("Type in some text: "))
>> number = int(input("How many times should it be printed? "))
>>
>> Traceback (most recent call last):
>> File "test4.py", line 2, in <module>
>>   number = int(input("How many times should it be printed? "))
>> ValueError: invalid literal for int() with base 10:
>> -------------------------------------------
>>
>> Now carefully read the message "invalid literal for int()". This tells you
>> the argument to int is not a string representation of an integer. There is
>> nothing wrong with input()!
>>
>> Since we don't know what you typed at the input() prompt we can't diagnose
>> it further. One way to solve problems like these is to capture and print the
>> output of input():
>>
>> text = str(input("Type in some text: "))
>> snumber = input("How many times should it be printed?")
>> print snumber
>>
>> A good robust alternative is to try int() and capture / report the
>> failure:
>>
>> text = str(input("Type in some text: "))
>> snumber = input("How many times should it be printed?")
>> try:
>>  number = int(snumber)
>> except ValueError:
>>  print(number, "is not an integer.")
>> else:
>>  print (text * number)
>>
>> --
>> Bob Gailer
>> 919-636-4239
>> Chapel Hill NC
>>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
This is a known bug with Komodo (see
http://bugs.activestate.com/show_bug.cgi?id=71319).
The developers are working on it, by the looks of things.

-- 
Rich "Roadie Rich" Lovely

Just because you CAN do something, doesn't necessarily mean you SHOULD.
In fact, more often than not, you probably SHOULDN'T.  Especially if I
suggested it.

10 re-discover BASIC
20 ???
30 PRINT "Profit"
40 GOTO 10


More information about the Tutor mailing list