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 8420977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:00:39+00:00 2026-06-10T03:00:39+00:00

Is there any way to generate a number of raw_inputs (With unique variables) based

  • 0

Is there any way to generate a number of raw_inputs (With unique variables) based on user input? So, say I had this:

if choice == 1:
    noelemen = int(raw_input("Enter total amount of elements: "))

Would there be any way to make it so the integer put in that raw_input field would then “generate” the required amount of raw_inputs? I’d suppose, if it was possible, that it’d make use of functions or something similar, but I’m a bit confused about how I’d get it done to be able to do that.

What I have currently is this:

if noelemen == 1:
        first = raw_input("Enter element: ")

        #Look for the weight of the entered element
        weight1 = float(elemen_data.get(first.lower()))

        if weight1 is not None:
            total_weight = weight1
        print "Total mass =", total_weight

if  noelemen == 2:
        first = raw_input("Enter first element: ")
        second = raw_input("Enter second element: ")

        #Look for the weight of the entered element
        weight1 = float(elemen_data.get(first.lower()))
        weight2 = float(elemen_data.get(second.lower()))

        if weight1 is not None:
            total_weight = weight1 + weight2
        print "Total mass =", total_weight

Which is probably a pretty messy way of doing it, particularly as I’d have to go up to perhaps something like 10 elements, or perhaps even beyond.

So, to repeat… Any way to generate raw_inputs with unique variables based on user input?

  • 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-10T03:00:41+00:00Added an answer on June 10, 2026 at 3:00 am

    how about something like this?

    elements = []
    numberOfPrompts = raw_input("Enter total amount of elements: ")
    for i in range(numberOfPrompts):
       # will prompt "Enter Element 1: " on the first iteration
       userInput = raw_input("Enter Element %s" % (i+1, )) 
       elements.append(userInput)
    

    Example case:

    >>> Enter total amount of elements: 2 # now hit enter
    

    at this point, the value of the variable numberOfPrompts will be 2.
    The value of the variable elements will be [], i.e. it is an empty list

    >>> Enter Element 1: 3.1415 # hit enter
    

    numberOfPrompts stays 2,
    elements will be ['3.1415']

    >>> Enter Element 2: 2.7182
    

    elements will be ['3.1415', '2.7182']

    Now the for-loop is done and you got your user inputs conveniently in the 0-indexed list elements, which you access like a tuple (array):

    >>> elements[1]
    2.7182
    

    Edit:

    After reading your comment I noticed what you intend to do and, just like the other answer stated, it would be best to use a dictionary for this. This should work:

    elements = {}
    numberOfPrompts = raw_input("Enter total amount of elements: ")
    for i in range(numberOfPrompts):
       # will prompt "Enter Element 1: " on the first iteration
       userInput = raw_input("Enter Element %s" % (i+1, )) 
       userInput = userInput.lower()
       elements[userInput] = float(elem_data.get(userInput))
    

    now elements will look like this:

    {'oxygen':15.9994, 'hydrogen':1.0079}
    

    you can iterate over all keys like this (to find out, which elements have been entered):

    for element in elements.keys():
        print element
    # output:
    oxygen
    hydrogen
    

    To get all values (to sum them up, for instance), do something like this:

    weightSum = 0
    for weight in elements.values():
        weightSum += weight
    print weightSum
    # output:
    17,0073
    

    Keep in mind that this example is for python 2.x. For python 3.x you will need to adjust a couple of things.

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

Sidebar

Related Questions

Is there any way to dynamically generate an swf file? Or insert variables after
Is there any way I could generate a four digit number from the AutoGenerated
Is there any way with which I can generate bit mask of a number
Is there any way to generate any html element on run time? Let's say,
Is there any (reasonably) straightforward way to generate comments to explain the structure of
Scrapy framework has the built-in capability to generate thumbnails. Is there any way to
Is there any way to use Google's API to retrieve a user's current zipcode
In HLSL, is there any way to limit the number of constant registers that
Is there any way to randomly generate a set of positive numbers such that
Is there any way in Perl to generate file handles programmatically? I want to

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.