Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking multiple inputs from user in python

I know how to take a single input from user in python 2.5:

raw_input("enter 1st number")

This opens up one input screen and takes in the first number. If I want to take a second input I need to repeat the same command and that opens up in another dialogue box. How can I take two or more inputs together in the same dialogue box that opens such that:

Enter 1st number:................
enter second number:.............
like image 695
user899714 Avatar asked Sep 11 '11 12:09

user899714


3 Answers

This might prove useful:

a,b=map(int,raw_input().split())

You can then use 'a' and 'b' separately.

like image 167
anurag619 Avatar answered Oct 21 '22 07:10

anurag619


How about something like this?

user_input = raw_input("Enter three numbers separated by commas: ")

input_list = user_input.split(',')
numbers = [float(x.strip()) for x in input_list]

(You would probably want some error handling too)

like image 29
jcfollower Avatar answered Oct 21 '22 09:10

jcfollower


Or if you are collecting many numbers, use a loop

num = []
for i in xrange(1, 10):
    num.append(raw_input('Enter the %s number: '))

print num
like image 4
Jakob Bowyer Avatar answered Oct 21 '22 09:10

Jakob Bowyer