-3

Just started programming in python 3 and I am trying to pull from an input() where I have placed the input() command in the first line and then further down use the output() to retrieve the input().

Here is an example:

rent = eval(input("How much does rent cost per year? $"))

Now I want to get the input I put in (10,000) and retrieve it from the input automatically using output() or another command.

print output("The family rent is", _______ , "per year." 

What code would go in the ________ so I can retrieve what I put in for the input?

Thanks - newbie

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
John B
  • 3
  • 2
  • 1
    OH please, please don't ever use `eval`. Especially not directly on user input! – DeepSpace Jul 10 '18 at 18:22
  • Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – DeepSpace Jul 10 '18 at 18:24
  • I searched all over and didn't see the answer. I looked at your possible duplicate @DeepSpace and get a better understanding but not the answer I was looking for. – John B Jul 10 '18 at 18:31
  • @DeepSpace Thanks for editing and clarifying the eval( command. I built up a huge code writing more detailed eval( and using arithmetic, but since I got 3 negative votes, I cant ask questions for several days. Wish I could pm you but its blocked. Thanks for all your help. – John B Jul 12 '18 at 13:09

2 Answers2

0

Use string formatting:

print('The family rent is {} per year'.format(rent))

See here for documentation of string formatting: https://docs.python.org/3/library/string.html#format-string-syntax

  • I want to use the input() so when I print it will prompt for the input with the question. Then after the input is put in (example 10,000) , I want to be able to retrieve the exact input I put in (10,000) to be recalled in an ouput().. Thanks. – John B Jul 10 '18 at 18:36
  • The input function returns exactly what you entered (as a string), so in your code above the variable `rent` will contain your input. You can then pass `rent` to any function to use it, work with it, print it out or whatever. Is that what you're looking for? –  Jul 10 '18 at 18:58
0

the code as is :

# get user input
user_input = input("How much does rent cost per year? $")
# cast to int or whaterver you're expecting
try :
    cost = int(user_input)
except :
    print ("expecting a number") 
    # stop here maybe return
# print output
print ("The family rent is", cost , "per year.")