[Tutor] Why do I get an "unvalid syntax" on the print line for number1

Prasad, Ramit ramit.prasad at jpmorgan.com
Tue Apr 23 20:27:07 CEST 2013


Pat Collins wrote:
> Any help appreciated.
> 
> 
> #!/usr/bin/env python
> """
> number.py Demonstrates collecting a number from the user.
> """
> 
> number_string1 = float(input("Give me a number: "))
> number1 = (number_string1)

number_string1 is not a string as you have already converted
it to a float. number1 will be the same as number_string1
and so this line is pointless.

> 
> number_string2 = float(input("Give me another number: "))
> number2 = (number_string2)
> 
> print number1, "times", number, "+", number1 * number2

It is always to your benefit to provide Python version, operating
system, and a full error message including trace (copy/paste,
do not paraphrase).

>>> '2' sdfs 2
  File "<input>", line 1
    '2' sdfs 2
           ^
SyntaxError: invalid syntax

When it comes to syntax errors Python will usually denote where
the error occurs with a "^". The error will be at or before 
the "^". Occasionally it will be on the previous line; typically
where a missing end bracket/brace/parenthesis.

I suspect you are on Python 3 and need to use print as a 
function. If not, please include the full error message.

print(number1)


Note: If you are on Python 2, you should use raw_input() 
and not input().

~Ramit






This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list