Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2.7 taking input from user for two separate variables

getMin, getMax = int(input("Enter a range (min,max): "));

Above is the code I am trying to implement but it gives me an error saying...

int() argument must be a string or a number, not 'tuple'

Basically, I have tried entering .split(,) after the input statement but I get the same error. When a user enters 1,10 as an input I want getMin = 1 and getMax = 10

like image 200
AMcCauley13 Avatar asked Sep 26 '26 08:09

AMcCauley13


2 Answers

Since you're on Python 2, you should be using raw_input instead of input. Try something like this:

from ast import literal_eval

prompt = "Enter a range (min,max): "
getMin = getMax = None

while {type(getMin), type(getMax)} != {int}:
    try:
        getMin, getMax = literal_eval(raw_input(prompt))
    except (ValueError, TypeError, SyntaxError):
        pass  # ask again...
like image 156
wim Avatar answered Sep 28 '26 21:09

wim


IMHO, cleaner approach assuming you want the input to be comma separated

>>> prompt = "Enter a range (min,max): "
>>> while True:
...     try:
...             getMin, getMax = map(int, raw_input(prompt).split(','))
...             break
...     except (ValueError, TypeError):
...             pass
... 
Enter a range (min,max): skfj slfjd
Enter a range (min,max): 232,23123,213
Enter a range (min,max): 12, 432
>>> getMin
12
>>> getMax
432
like image 21
balki Avatar answered Sep 28 '26 20:09

balki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!