[Tutor] Input() is not working as expected in Python 3.1
Yaraslau Shanhin
yaraslau.shanhin at gmail.com
Mon Feb 15 16:15:12 CET 2010
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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100215/d02e90e3/attachment.htm>
More information about the Tutor
mailing list