lundi 12 novembre 2018

For loop with raw_input, If-statement should increase input by 1 [duplicate]

This question already has an answer here:

everybody! I am currently learning Python (no prior coding skills - at least not enough to mention them) and I am struggling with a for loop in a lottery machine we are supposed to do for homework. I have spent the past two hours googling this, but I wasn't able to find anything that would hint me in the right direction.

I want the loop to create a list with random numbers. The amount of random numbers printed to the user should be defined by user input and it should not use duplicates. Every time a duplicate is created it should simply pick a different number. Any hints on where i screwed up here? (Pls note it's Python 2.x)

Thank you all! :)

Code:

from random import randint

lotterylist = []

print "Welcome to the lottery machine!"

mynumbers =  int(raw_input("How many numbers should be drawn?\n"))

for i in range(0, mynumbers):

    lottery_numbers = randint(0, 48)

    if lottery_numbers not in lotterylist:
        lotterylist.append(lottery_numbers)

    else:
        mynumbers += 1
        continue

print lotterylist

print "End"




Aucun commentaire:

Enregistrer un commentaire