Getting input from user

This is the place for queries that don't fit in any of the other categories.

Getting input from user

Postby sg5176 » Sat Nov 23, 2013 9:41 am

Hi,I'm a python beginner.i'd like to know how to get the inputs from user to this simple question

Write a program to find the distance, d, between points A and B
where the positions of A and B are specified by the Cartesian coordination of (x1, y1) and (x2, y2), respectively.

Enter the first point: 1 1
Enter the second point: 5 5
The distance between (1, 1) and (5, 5) is 5.66.

actually the calculation is easy but i don't know how to get the inputs from user
is there anyway to get multiple inputs in one line ?
any one please help, thanks
Last edited by Mekire on Sat Nov 23, 2013 10:05 am, edited 1 time in total.
Reason: First post lock. Please Read "New Users Read This"
sg5176
 
Posts: 2
Joined: Sat Nov 23, 2013 9:33 am

Re: Getting input from user

Postby Mekire » Sat Nov 23, 2013 10:07 am

Of course this is possible, and of course we can assist you... but...
First we ask that you show us any attempts you have made and clarify exactly where you are having the problem. If the problem is "I haven't done anything and I don't know how to start," then it is likely we can't help.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Getting input from user

Postby stranac » Sat Nov 23, 2013 10:26 am

You can't get multiple inputs on a single line.
You can, however, create multiple values from the string returned by raw_input():

Code: Select all
>>> user_input = raw_input('Enter your stuff: ')
Enter your stuff: 1 2
>>> user_input
'1 2'
>>> user_input.split()
['1', '2']
>>> a, b = user_input.split()
>>> a
'1'
>>> b
'2'
>>> int(a)
1
>>> int(b)
2
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: Getting input from user

Postby sg5176 » Sat Nov 23, 2013 1:15 pm

Code: Select all
>>> y=input()
1 5
>>> y.split()
['1', '5']
>>> c,d = y.split()
>>> import math
>>> output=math.sqrt((a-c)**2+(b-d)**2)
Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    output=math.sqrt((a-c)**2+(b-d)**2)
TypeError: unsupported operand type(s) for -: 'str' and 'str'


I now have a problem that when i split ,the input is not integer so i cannot calculate.
sg5176
 
Posts: 2
Joined: Sat Nov 23, 2013 9:33 am

Re: Getting input from user

Postby stranac » Sat Nov 23, 2013 1:32 pm

Yes, use int() to convert them to integers.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests

cron