r/pythonhelp • • Sep 05 '19

Please help, I'm in an intro to programming class and I'm stuck on a question.

I've googled everything I could think of with no good results, this is the question:

For this programming exercise, write a simple script that will ask the user to input an integer value, wait for the user to enter the value, and then display the entered value back to the terminal. After that, the script should display two (2) times the value entered to the terminal as well.

Example script output (input in bold):

Please enter an integer: 42 You entered 42 Twice that value is 84

Please help, I don't just want the answer. I'm looking to understand it. Any help is much appreciated. Thanks

0 Upvotes

3 comments sorted by

2

u/cndvcndv Sep 05 '19 edited Sep 05 '19

user_input = input("Please enter an integer: ")

print("You entered " + user_input)

print("Twice that number is " + str(2*int(user_input)))

I think it's pretty straight forward but a little explanation: First line uses the input function to print "please enter an integer" and then assigns the input into a variable called user_input. You should keep in mind that user_input is a string now. Second line concatenates "you entered " with user_input and then prints it. In the third line, user_input is converted to an integer first, because we can't multiply strings with numbers, then it is multiplied by 2 and converted back to a string, because we need to concatenate it with the string "twice that number is ".

1

u/[deleted] Sep 05 '19

Thank you!!! This actually helped a lot.

1

u/jeffoBZ Sep 05 '19

Alright how this works is you need to turn your input into a a integer so to do that put int before the input so it should look like this variable = int(input("your question text")) then you can use the variable as a number so you can do double =variable*2 and that will do it for you. Then just do print ("your double is",double) hope this helped!