Reputation: 117
I'm trying to create a tuple from input where the values are two digits.
tup = tuple(input("enter a tuple"))
but when I enter something like 10 11
, I get this:
('1', '0', ' ', '1', '1')
The intended result is this:
(10, 11)
Upvotes: 1
Views: 1688
Reputation: 2866
I'm sure I'm adding in an additional step here, but this works.
Val= "11 12"
Val=Val.split(" ")
Val=[int(x) for x in Val]
tuple(Val)
or if you want to one line it:
tuple([int(x) for x in raw_input("enter a tuple:").split(" ")])
Edit
You could also just use the input
function if you're using python 2.7.
Tup=input("enter tuple:")
>(11,12,13)
print(Tup)
(11, 12, 13)
But I believe that executing user input for a program is considered risky and unacceptable.
Upvotes: 0
Reputation: 690
Try this:
tup = tuple(int(n) for n in input("Enter a tuple: ").split(" "))
This will work for integers of any number of characters as well as negative integers.
Upvotes: 2