-1

I'm a newbie of Python. In the following code(or see attached), I want to call all the numbers input inside a string in the last step of the program. How should I do that?

print "how old are you?",
age = raw_input()
print "how tall are you?",
height = raw_input()

print = (expected outcome: You're X old, X tall, etc.)
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
mag paige
  • 29
  • 5
  • https://pyformat.info/#simple – Akshat Mahajan Nov 12 '16 at 00:33
  • Python has a [`str.format`](https://docs.python.org/3.5/library/stdtypes.html?highlight=str.format#str.format) function that allows you to format strings using variables. – AChampion Nov 12 '16 at 00:35
  • `print("expected outcome: You're " + str(age) + " old, " + str(height) + " tall, etc.")` for python3 or `print "expected outcome: You're " + str(age) + " old, " + str(height) + " tall, etc."` for python2 – John Smith Nov 12 '16 at 00:38

3 Answers3

1

one simple way that i prefer is to use the string formatting operator like this:

>>> age = 10
>>> height = 72
>>> print "you're %s years old and %s cm tall" % (age, height)
you're 10 years old and 72 cm tall
>>>
matias elgart
  • 1,123
  • 12
  • 18
0

you can print as many thing as you want for example:

print "You're", age, "years old and", height, "centimeters tall"

or you can use string format

print "You're {} years old and {} centimeters tall".format(age,height)

example

>>> age=29
>>> height=160
>>> print "You're", age, "years old and", height, "centimeters tall"
You're 29 years old and 160 centimeters tall
>>> print "You're {} years old and {} centimeters tall".format(age,height)
You're 29 years old and 160 centimeters tall
>>> 

there are several ways on how to do it as illustrated in the others answers and comments, my favorite is the string format

Also if you need to convert those input in numbers to do math on them use int or float accordingly

x = int(raw_input("write a number: "))

furthermore, as you are learning python I recommend starting with the newest version, python 3.5 you can later ajust to older version if needed

Copperfield
  • 8,131
  • 3
  • 23
  • 29
0

Cool!, welcome to coding in python!, there are lots of ways to do what you want, i'll show you one, but feel free to look for another

print "how old are you?",
age = raw_input()
print "how tall are you?",
height = raw_input()

print("You're " + age + " old, " + height + " tall, etc.")

EXTRA: you can add params to raw_input and make it like:

age = raw_input("how old are you?")
height = raw_input("how tall are you?")

print("You're " + age + " old, " + height + " tall, etc.")
aleivag
  • 2,306
  • 1
  • 13
  • 13
  • Thanks! What adjustment can I make in the last line "print ---" in order to make a number that is called followed by a comma? – mag paige Nov 12 '16 at 11:45
  • sorry did not understand the question, what do you want as output? – aleivag Nov 14 '16 at 07:11
  • print "how old are you?", age = raw_input() print "how tall are you?", height = raw_input() print "how much do you want?", cash = raw_input() print("You're " + age + " old, " + height + " tall," + " want " + cash) I would like the output to be: You're 22 old, 160 tall, want 99999. I am not sure how to put a full stop after the input '99999'. – mag paige Nov 14 '16 at 14:12
  • maybe by `full stop` you meant `dont put a new line at the end`, that you can do (i'm assuming you are ussing python2.7 its the same on python3 but there is also a easy way in python3). you can `import sys; sys.stdout.write("You're " + age + " old, " + height + " tall," + " want " + cash)` (NOTE: if this is what you want, you can import sys only once). let me know if this what you meant – aleivag Nov 14 '16 at 18:00