Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Create a list of tuples from user input
- # Sample input:- (1, 4, -7), (4, -2, 5), (-6, 1, 9)
- while True:
- valid = True
- userin = input("Enter a series of tuples: ")
- # Replace all white space in the tuple
- inlist = userin.split()
- userin = ''.join(inlist)
- # Now split on ")" (which seems to be the most reliable indicator of end of tuple)
- tlist = userin.split(')')
- reslist = []
- for x, t in enumerate(tlist):
- t = t.strip(',')
- t = t.strip('(')
- nlist = t.split(',')
- tuplist = []
- for sn in nlist:
- if not sn:
- continue
- try:
- n = int(sn)
- tuplist.append(n)
- except:
- valid = False
- break
- if tuplist:
- reslist.append(tuple(tuplist))
- if valid:
- break
- print("Invalid input. Re-enter\n")
- continue
- print(reslist)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement