0

Im trying to generate from random variables for an inserting in a database but I'm always getting the same string when i try to iterate thru the widgetname list. I'm getting gadget all the 5 iterations. How do i get different string?

output Im getting

wd name: gadget
91
9
wd name: gadget
87
1
wd name: gadget
41
10
wd name: gadget
16
10
wd name: gadget
50
4 

my code:

widgetname=['sprocket', 'gizmo', 'gadget']
price=0
design_date='2010-02-10'
version=0
design_comment='design comment goes here'

count = 0
while (count < 5):
    count += 1
    for widget in widgetname:
        widget_name = widget
    for i in range(count):
        price=random.randint(1.0, 100.0)
    for j in range(count):
        version=random.randint(1.0, 10.0)

    print('wd name: '+widget_name)
    print(price)
    print(version)
6
  • 1
    your for loops are not nested, they're sequential Commented Nov 3, 2017 at 21:57
  • What's the point of your inner loops anyways? All three of them create 5 values but only store the last. Each loop completes completely before the next one starts. Throw some print statements into the loops to understand what your code does. Commented Nov 3, 2017 at 21:57
  • Remove: for i in range(count): and for j in range(count):, and indent the print statements. Commented Nov 3, 2017 at 21:59
  • How many output groups do you actually want? 5? or 5x5x5x5 = 625? Commented Nov 3, 2017 at 21:59
  • If you're trying to get a random widget, you don't need a for loop but random.choice. Commented Nov 3, 2017 at 22:00

1 Answer 1

3

You don't need all the loops, just the outer one, see the snippet below

for count in range(5):
    widget_name = random.choice(widgetname)   
    price=random.randint(1, 100)
    version=random.randint(1, 10)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, its so simple, was unnecessarily complicating it.

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.