Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8433857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:30:08+00:00 2026-06-10T06:30:08+00:00

The code given works fine but not with the original code. I would like

  • 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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:30:10+00:00Added an answer on June 10, 2026 at 6:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a code for pinging of given URL.It works fine for me.But whenever
The following code snippet works fine in 1.8.7 on Mac OS X, but not
document.location() function given in the logn.js code works fine in Internet Explorer but does
Below code works fine in Firefox, but not in IE8. It triggers mouse click
This code works great for generating thumbnails, but when given a very large (100MB+)
I was given a c++ main and have to code it so it works.
In cygwin, the following code works fine $ cat junk bat bat bat $
The following code gives seg fault and some time stalls/hangs and never works. for(i=0;i<360;i++)
I can't get this code to work. Declared playpauseButton just above the given below
I'm indexing and searching code using a custom analyzer. Given text will wi-fi work,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.