-2

I am trying to write a simple script that prints the user's input but I cannot get the print function to work. I believe it is something very small that I am missing. Below is an example I found outline but the script ends after I enter a number. Thanks!

word = input("Tell me your age: ")  
num = int(word)  
print("Your age is ", num)

Update: Print works when the input and print are entered separately. Why can't I enter it all together?

>>>word = input("Tell me your age: ")

>>>print "Your age is ", word

Tell me your age: 10

>>> word = input("Tell me your age: ")

Tell me your age: 10

>>> print "Your age is ", word

Your age is 10

Q Pitt
  • 1
  • 1
  • 5
    Do you get any error message, or simply nothing happens? – FlyingTeller Mar 11 '19 at 16:42
  • no need to convert to int in python 2.7. Input evaluates the result (which is also a security issue). I see nothing suspicious in this piece of code. – Jean-François Fabre Mar 11 '19 at 16:44
  • In python 2, `print` is a statement, not a function, and you are printing a `tuple`. You should really just use Python 3 (it seems like you are learning Python). – juanpa.arrivillaga Mar 11 '19 at 16:45
  • If you are only just learning the basics, you should probably ignore Python 2, and spend your time on the currently recommended and supported version of the language, which is Python 3. – tripleee Mar 11 '19 at 16:47
  • I am kinda sure `input` does not exist in python2, is it just a tag mistake – BlueSheepToken Mar 11 '19 at 16:48
  • @FlyingTeller No error, just nothing happens. – Q Pitt Mar 11 '19 at 16:51
  • Possible duplicate of [What is the difference between print and print() in python 2.7](https://stackoverflow.com/questions/33996749/what-is-the-difference-between-print-and-print-in-python-2-7) – Nick is tired Mar 11 '19 at 17:02
  • `input` does exist in Python 2; it tries to evaluate its input as a Python expression. Nomally in Python 2 you'd want `raw_input` which does what `input` does in Python 3, i.e. simply return the user's input string verbatim. – tripleee Mar 11 '19 at 17:16

5 Answers5

0

You indicated that you are using Python 2.7, in which case you should be using raw_input to read input to a string, rather than input. I can't say for sure if this is what causes your problem, but it definitely jumps out.

Christoph Burschka
  • 4,467
  • 3
  • 16
  • 31
0

The example you copied will work in Python3. The input statement works, but you do not print correctly. If you want it to work in Python2.7 try something like:

>>> num = 34
>>> print "Your age is", num
Your age is 34
neun
  • 11
  • 1
  • 2
0

I believe you're referring to the brackets being printed along with your printed statement?

>>> print("Your age is ", num)
('Your age is ', 27)

The reason for this is that print does not require brackets on python2. this is python3 syntax. Giving brackets with two values makes python treat your string like a tuple.

>>> print "Your age is ", num
Your age is  27

Remove the brackets and it will run fine.

0

The answer is very simple. In Python 3 print is a function, so it has usage like:

print('Your age is ', num)

But in Python 2.7 syntax for print is like:

print 'Your age is', num

So you need to write:

word = input("Tell me your age: ")  
num = int(word)  
print "Your age is", num
0

This will works fine in Python 3.6:

>>> age = int(input("Tell me your age: "))
Tell me your age: 27
>>> print("Your age is ", age)
Your age is  27

And the following in Python 2.7:

>>> age = int(input("Tell me your age: "))
Tell me your age: 27
>>> print "Your age is ", age
Your age is  27
maikynata
  • 1
  • 2