3
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

1

3 Answers 3

1

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...
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

the input function (see doc here) will try to evaluate the provided input. For example, if you feed it with your name AMcCauley13 it will look for a variable named so. Feeding it with values like 1,10 will evaluate in a tuple (1,10), which will break the int() function that expects a string.

Try with the simpler

getMin = int(raw_input("min range: "))
getMax = int(raw_input("max range: "))

or combining split and map as balki suggested in the meanwhile

3 Comments

thank you! I had it done this way initially but did not know if my professor would accept it done that way.
How about don't use input at all, so you don't have to deal with strings like __import__('os').system('cat /etc/passwd').
that's a good point. Fixing it to avoid misleading others that may use it for something more than an assignment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.