Apr-09-2019, 01:54 PM
(Apr-09-2019, 12:37 PM)souprqtpie Wrote: could you explain the replace bit to me in a little more detail please showing how it works. ( I am trying to keep both numbers on the same input line). I've done the while true loop before, but I didn't know how to get it to measure both numbers at once without making a separate loop for each one
>>> value = '2, 3' # user enters with a comma >>> value.split() # comma is retained by split() ['2,', '3'] >>> [int(v) for v in value.split()] # comma prevents converting to integer Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 10: '2,' >>> value.replace(',', ' ') # replace gets rid of the comma '2 3' >>> value.replace(',', ' ').split() # you can chain it with split, to get the two numbers entered ['2', '3'] >>> [int(v) for v in value.replace(',', ' ').split()] # now you can convert to integer [2, 3]In terms of measuring two numbers at once, it depends on what you are measuring. If you are just checking a range, you can use min and max:
if min(values) < low or max(values) > high: print('Please enter numbers in the range {} to {}.'.format(low, high))If you are checking them with a function that returns True if the number is valid, you can use map and all:
if all(map(check_function, valid)): print('All numbers are valid')Map will pass each number in valid as a parameter to check_function, and return a list of all the return values. All will return true if all the values in a sequence evaluate to True, and False if any of them don't.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures