[Tutor] Input() is not working as expected in Python 3.1
Wayne Werner
waynejwerner at gmail.com
Mon Feb 15 15:48:45 CET 2010
On Mon, Feb 15, 2010 at 8:23 AM, Yaraslau Shanhin <
yaraslau.shanhin at gmail.com> wrote:
> Hello All,
>
> I am working with Python tutorial in wiki and one of the exercises is as
> follows:
>
> Ask the user for a string, and then for a number. Print out that string,
> that many times. (For example, if the string is hello and the number is 3 you
> should print out hellohellohello.)
>
> Solution for this exercise is:
>
> text = str(raw_input("Type in some text: "))
> number = int(raw_input("How many times should it be printed? "))print (text * number)
>
>
>
> Since in Python raw_input() function was renamed to input() according to PEP 3111 <http://www.python.org/dev/peps/pep-3111/> I have respectively updated this code to:
>
>
> text = str(input("Type in some text: "))
>
>
This str() is redundant - input returns a string by default.
>
> number = int(input("How many times should it be printed? "))print (text * number)
>
>
>
> However when I try to execute this code in Python 3.1 interpreter error message is generated:
>
>
> Type in some text: some
> How many times should it be printed? 3
> 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? 3'
>
>
That means you're having an issue with getting something that isn't just a
number.
try:
number = input("How many ... ")
print (number)
number = int(number)
print (number)
HTH,
Wayne
>
> Can you please advise me how to resolve this issue?
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn’t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100215/0af75f1c/attachment.htm>
More information about the Tutor
mailing list