Howie
Howie

Reputation: 31

If statement to control user input

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)

Upvotes: 0

Views: 29582

Answers (6)

Daniel Benites
Daniel Benites

Reputation: 1

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'))

Upvotes: 0

Levon
Levon

Reputation: 143047

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.

Upvotes: 2

Oleksii Kachaiev
Oleksii Kachaiev

Reputation: 6234

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() 

Upvotes: 2

Tim Pietzcker
Tim Pietzcker

Reputation: 336128

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)

Upvotes: 2

Antimony
Antimony

Reputation: 2240

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')

Upvotes: 1

Lanaru
Lanaru

Reputation: 9711

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!")

Upvotes: 2

Related Questions