0

The code given works fine but not with the original code. I would like the code to offer a number 1 through 5 and only accept a number 1 through 5. The number choosen Within range would still return random integers.

import random

user_input = raw_input("Enter a number between 1 and 5 : ") 
selected_elem = []

while len(selected_elem) < int(user_input):
    if user_input >= int(6):
        print ("That is not an option...")
    random_elem = random.randrange(1, 10, 1)
    if random_elem not in selected_elem:
        selected_elem.append(random_elem)

print ("Here are the numbers... ")+ str(selected_elem)
0

6 Answers 6

2

UPDATE with OP code posted now:

while len(selected_elem) < int(user_input):
    if 1 <= int(user_input) <= 5:
        random_elem = random.randrange(1, 10, 1)
        if random_elem not in selected_elem:
            selected_elem.append(random_elem)
    else:
        print ("That is not an option...")

Note that if the input to user_input is not a number and int() fails your program will crash (throw an exception). You can graft on the try/except code shown below to deal with that if necessary.

------ Previous answer w/o code posted by OP ------------

If you can be sure the input will always be a number you can do this:

while True:
    num = input('Enter number between 1 - 5:')
    if 1 <= num <= 5:
        print 'number is fine' 
        break
    else:
        print 'number out of range'

It will continue to loop until the user enters a number in the specified range.

Otherwise, the added try/except code will catch non-numeric input:

while True:
    try:
        num = input('Enter number between 1 - 5:')
        if 1 <= num <= 5:
            print 'number is fine'
            break
        else:
            print 'number out of range'
    except NameError:
        print 'Input was not a digit - please try again.'

If you don't want the user to try again after entering a non-number, just adjust the message and add a break statement below the final print to exit the loop.

Sign up to request clarification or add additional context in comments.

4 Comments

if the int(user_input) is needed then the if 1 <= etc line won't work.
I mean that if user_input is a string, requiring an int() call, then you can't compare it to integers in the next line.
@DSM ah .. I see what you mean .. right. I guess I'll have to do another int() call .. ugly.
Hey thanks for the info, I didnt know that. I'm still new to this site and to python altogether so my apologies.
2

Here's my attempt to scrape together an answer based off your limited details:

num = input('Please enter a number between 1-5')

if num in range(1,6):
    print("That's a valid option. Congratulations!")
else:
    print("That's not an option, fool!")

Comments

2

For the heck of it:

import re
def ask_digit():
    while True:
        digit = raw_input("Please enter a number between 1 and 5: ")
        if re.match(r"[1-5]$", digit):
            return int(digit)

Comments

2

Not very clear description, but it seems that you need something like:

def ask_digit(calls=0):
    if calls > 10:
        print "You are so boring..."
        raise ValueError("Can't get answer from user")

    try:
        num = int(raw_input("Enter number 1-5: "))
    except ValueError:
        print "Not a digit"
        return ask_digit(calls+1)

    if num < 1 or num > 5:
        print "Not valid"
        return ask_digit(calls+1)

    return num

if __name__ == "__main__":
    ask_digit() 

3 Comments

Hmm. Let's see how many wrong attempts I can enter before I blow Python's stack :)
@TimPietzcker It's not hard to check count of calls :)
This seems like an overcomplicated and fragile way to implement a for loop with a break.
1

Here's an answer that keeps prompting the user to enter a number, until it finally gets a valid number.

n = input('Enter a number in the range 1-5')

while(n < 1 or n > 5):
    print('Incorrect input')
    n = input('Enter a number in the range 1-5')

Comments

0

you have to put the int in the input for the code to work

n = int(input('Enter a number in the range 1-5'))
while(n < 1 or n > 5):
    print('Incorrect input')
    n = int(input('Enter a number in the range 1-5'))

Comments

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.