[Tutor] Issue with string method: str(variable ** n)

2013-05-16 Thread Rafael Knuth
Hej,

I wrote a tiny little program which I was hoping would take a number as
input, square and print it:

square = input ("Enter a number. ")
print (str(square) + " squared is " + str(square ** 2))

It seems I can't work with variables within the str() string method, and I
was wondering if anyone can help?

PS. I am using Python 3.3.0

Thank you in advance!

Rafael
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Issue with string method: str(variable ** n)

2013-05-16 Thread Dave Angel

On 05/16/2013 02:58 PM, Rafael Knuth wrote:

Hej,

I wrote a tiny little program which I was hoping would take a number as
input, square and print it:

square = input ("Enter a number. ")
print (str(square) + " squared is " + str(square ** 2))

It seems I can't work with variables within the str() string method, and I
was wondering if anyone can help?

PS. I am using Python 3.3.0

Thank you in advance!

Rafael


In Python 3.3.0, input returns a string.  So square is a string.  There 
isn't any meaning to squaring a string.



You probably want either:

square = float(input("Enter a number.")
or
square = int(input("Enter a number.")

Suggestion for next time - include the error traceback.

--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Issue with string method: str(variable ** n)

2013-05-16 Thread Zachary Ware
On Thu, May 16, 2013 at 1:58 PM, Rafael Knuth  wrote:
> Hej,

Hi Rafael,

> I wrote a tiny little program which I was hoping would take a number as
> input, square and print it:
>
> square = input ("Enter a number. ")
> print (str(square) + " squared is " + str(square ** 2))
>
> It seems I can't work with variables within the str() string method, and I
> was wondering if anyone can help?
>
> PS. I am using Python 3.3.0

In the future, it's always very helpful to post any tracebacks you
get, everything from "Traceback (most recent call last):" to the last
thing printed.

In this case, it seems that your problem is that in Python3, input()
returns the input as a string.  Python2's input() function would
actually evaluate the input, which was incredibly insecure.

You can fix your program by calling int() on square at the end of your
print call.  If I were writing this myself, I would do this, though:

number = int(input("Enter a number. "))
print("{} squared is {}".format(number, number**2))

You might find the tutorial page on Input and Output[1] instructive,
particularly about the format method I used above.

Hope this helps,

Zach

[1] http://docs.python.org/3/tutorial/inputoutput.html

>
> Thank you in advance!
>
> Rafael
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Issue with string method: str(variable ** n)

2013-05-16 Thread Rafael Knuth
Thank you - that makes perfectly sense.

Also, I am new to the list, and I appreciate your suggestion.
I will include error tracebacks in the future.

All the best,
Rafael


On Thu, May 16, 2013 at 9:14 PM, Dave Angel  wrote:

> On 05/16/2013 02:58 PM, Rafael Knuth wrote:
>
>> Hej,
>>
>> I wrote a tiny little program which I was hoping would take a number as
>> input, square and print it:
>>
>> square = input ("Enter a number. ")
>> print (str(square) + " squared is " + str(square ** 2))
>>
>> It seems I can't work with variables within the str() string method, and I
>> was wondering if anyone can help?
>>
>> PS. I am using Python 3.3.0
>>
>> Thank you in advance!
>>
>> Rafael
>>
>>
>>  In Python 3.3.0, input returns a string.  So square is a string.  There
> isn't any meaning to squaring a string.
>
>
> You probably want either:
>
> square = float(input("Enter a number.")
> or
> square = int(input("Enter a number.")
>
> Suggestion for next time - include the error traceback.
>
> --
> DaveA
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor