0

So I have this program that creates variables automatically depending on the number that the user inputted, but I need a way to call the variables again to retrieve the value from them.

This is what I have:

count = int(input("

for x in range(0, ):
                globals()['wordNumber%s' % x] = input("Word: ")
number = 1
while True:
    print (wordNumber.str(number))
    number = number + 1

For the print part, it tries to print just wordNumber which doesn't exist, so there is an error. I need it to somehow add wordNumber together with a number (1-5) so it can print(wordNumber1) or print(wordNumber3)

1
  • 1
    Why not just use an array? Commented Jan 31, 2015 at 17:35

1 Answer 1

2

Use a normal dict to store the data:

nums = {}
count = int(input("Enter range: "))
for x in range(0, count):
    nums['wordNumber%s' % x] = input("Word: ")
print(nums)

Demo:

Enter range: 2
Word: foo
Word: bar

In [5]: nums
Out[5]: {'wordNumber0': 'foo', 'wordNumber1': 'bar'}
In [6]: nums["wordNumber0"]
Out[6]: 'foo'
In [7]: nums["wordNumber1"]
Out[7]: 'bar'
Sign up to request clarification or add additional context in comments.

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.