2016-03-26 21 views
1

私はある量の要素をとり、それらをランダムに生成するプログラムを作ろうとしました。たとえば、ユーザーは20個の名前を入力し、ランダムにアルゴリズムを吐き出すことができます。私はあなたが最大20の要素を入力することができるような方法でプログラムを構築しました。そして、その数と1つのランダムなものを印刷します。だからあなたは20個の要素を入力することができ、無作為に21個の要素を選択します。問題は、私もすべてランダムで経験しているわけではないので、私はそれを馬鹿げたほど大きくしました(ちょうど500行に恥ずかしがります)。それは動作しますが、私は好きです: 1)それが短縮されるために。 2)2と20の間ではなく、任意の数をとる。3)ユーザが入力した数のランダムな項目を吐き出すことができる。私はそれが意味をなさないかどうかわからないが、できるなら助けて! 全体コード:大規模なPythonランダムシェアラー

import random 
a = input('Enter a number of elements(maximum twenty).\n') 
if a < 2 or a > 20: 
    print('Restart program and enter a value between two and twenty.') 
if(a == 2): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    list_one = [one,two] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    print(choice_one, choice_two, choice_three) 
if(a == 3): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    list_one = [one,two,three] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four) 
if(a == 4): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    list_one = [one,two,three,four] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five) 
if(a == 5): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    list_one = [one,two,three,four,five] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six) 
if(a == 6): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    six = raw_input('Enter the sixth element.\n') 
    list_one = [one,two,three,four,five,six] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    choice_seven = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six, choice_seven) 
if(a == 7): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    six = raw_input('Enter the sixth element.\n') 
    seven = raw_input('Enter the seventh element.\n') 
    list_one = [one,two,three,four,five,six,seven] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    choice_seven = random.choice(list_one) 
    choice_eight = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six, choice_seven, choice_eight) 
if(a == 8): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    six = raw_input('Enter the sixth element.\n') 
    seven = raw_input('Enter the seventh element.\n') 
    eight = raw_input('Enter the eighth element.\n') 
    list_one = [one,two,three,four,five,six,seven,eight,] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    choice_seven = random.choice(list_one) 
    choice_eight = random.choice(list_one) 
    choice_nine = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six, choice_seven, choice_eight, choice_nine) 
if(a == 9): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    six = raw_input('Enter the sixth element.\n') 
    seven = raw_input('Enter the seventh element.\n') 
    eight = raw_input('Enter the eighth element.\n') 
    nine = raw_input('Enter the ninth element.\n') 
    list_one = [one,two,three,four,five,six,seven,eight,nine] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    choice_seven = random.choice(list_one) 
    choice_eight = random.choice(list_one) 
    choice_nine = random.choice(list_one) 
    choice_ten = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six, choice_seven, choice_eight, choice_nine, choice_ten) 
if(a == 10): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    six = raw_input('Enter the sixth element.\n') 
    seven = raw_input('Enter the seventh element.\n') 
    eight = raw_input('Enter the eighth element.\n') 
    nine = raw_input('Enter the ninth element.\n') 
    ten = raw_input('Enter the tenth element.\n') 
    list_one = [one,two,three,four,five,six,seven,eight,nine,ten] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    choice_seven = random.choice(list_one) 
    choice_eight = random.choice(list_one) 
    choice_nine = random.choice(list_one) 
    choice_ten = random.choice(list_one) 
    choice_eleven = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six, choice_seven, choice_eight, choice_nine, choice_ten, choice_eleven) 
if(a == 11): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    six = raw_input('Enter the sixth element.\n') 
    seven = raw_input('Enter the seventh element.\n') 
    eight = raw_input('Enter the eighth element.\n') 
    nine = raw_input('Enter the ninth element.\n') 
    ten = raw_input('Enter the tenth element.\n') 
    eleven = raw_input('Enter the eleventh element.\n') 
    list_one = [one,two,three,four,five,six,seven,eight,nine,ten,eleven] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    choice_seven = random.choice(list_one) 
    choice_eight = random.choice(list_one) 
    choice_nine = random.choice(list_one) 
    choice_ten = random.choice(list_one) 
    choice_eleven = random.choice(list_one) 
    choice_twelve = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six, choice_seven, choice_eight, choice_nine, choice_ten, choice_eleven, choice_twelve) 
if(a == 12): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    six = raw_input('Enter the sixth element.\n') 
    seven = raw_input('Enter the seventh element.\n') 
    eight = raw_input('Enter the eighth element.\n') 
    nine = raw_input('Enter the ninth element.\n') 
    ten = raw_input('Enter the tenth element.\n') 
    eleven = raw_input('Enter the eleventh element.\n') 
    twelve = raw_input('Enter the twelth element.\n') 
    list_one = [one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    choice_seven = random.choice(list_one) 
    choice_eight = random.choice(list_one) 
    choice_nine = random.choice(list_one) 
    choice_ten = random.choice(list_one) 
    choice_eleven = random.choice(list_one) 
    choice_twelve = random.choice(list_one) 
    choice_thirteen = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six, choice_seven, choice_eight, choice_nine, choice_ten, choice_eleven, choice_twelve, choice_thirteen) 
if(a == 13): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    six = raw_input('Enter the sixth element.\n') 
    seven = raw_input('Enter the seventh element.\n') 
    eight = raw_input('Enter the eighth element.\n') 
    nine = raw_input('Enter the ninth element.\n') 
    ten = raw_input('Enter the tenth element.\n') 
    eleven = raw_input('Enter the eleventh element.\n') 
    twelve = raw_input('Enter the twelth element.\n') 
    thirteen = raw_input('Enter the thirteenth element.\n') 
    list_one = [one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    choice_seven = random.choice(list_one) 
    choice_eight = random.choice(list_one) 
    choice_nine = random.choice(list_one) 
    choice_ten = random.choice(list_one) 
    choice_eleven = random.choice(list_one) 
    choice_twelve = random.choice(list_one) 
    choice_thirteen = random.choice(list_one) 
    choice_fourteen = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six, choice_seven, choice_eight, choice_nine, choice_ten, choice_eleven, choice_twelve, choice_thirteen, choice_fourteen) 
if(a == 14): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    six = raw_input('Enter the sixth element.\n') 
    seven = raw_input('Enter the seventh element.\n') 
    eight = raw_input('Enter the eighth element.\n') 
    nine = raw_input('Enter the ninth element.\n') 
    ten = raw_input('Enter the tenth element.\n') 
    eleven = raw_input('Enter the eleventh element.\n') 
    twelve = raw_input('Enter the twelth element.\n') 
    thirteen = raw_input('Enter the thirteenth element.\n') 
    fourteen = raw_input('Enter the fourteenth element.\n') 
    list_one = [one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    choice_seven = random.choice(list_one) 
    choice_eight = random.choice(list_one) 
    choice_nine = random.choice(list_one) 
    choice_ten = random.choice(list_one) 
    choice_eleven = random.choice(list_one) 
    choice_twelve = random.choice(list_one) 
    choice_thirteen = random.choice(list_one) 
    choice_fourteen = random.choice(list_one) 
    choice_fifteen = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six, choice_seven, choice_eight, choice_nine, choice_ten, choice_eleven, choice_twelve, choice_thirteen, choice_fourteen, choice_fifteen) 
if(a == 15): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    six = raw_input('Enter the sixth element.\n') 
    seven = raw_input('Enter the seventh element.\n') 
    eight = raw_input('Enter the eighth element.\n') 
    nine = raw_input('Enter the ninth element.\n') 
    ten = raw_input('Enter the tenth element.\n') 
    eleven = raw_input('Enter the eleventh element.\n') 
    twelve = raw_input('Enter the twelth element.\n') 
    thirteen = raw_input('Enter the thirteenth element.\n') 
    fourteen = raw_input('Enter the fourteenth element.\n') 
    fifteen = raw_input('Enter the fifteenth element.\n') 
    list_one = [one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    choice_seven = random.choice(list_one) 
    choice_eight = random.choice(list_one) 
    choice_nine = random.choice(list_one) 
    choice_ten = random.choice(list_one) 
    choice_eleven = random.choice(list_one) 
    choice_twelve = random.choice(list_one) 
    choice_thirteen = random.choice(list_one) 
    choice_fourteen = random.choice(list_one) 
    choice_fifteen = random.choice(list_one) 
    choice_sixteen = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six, choice_seven, choice_eight, choice_nine, choice_ten, choice_eleven, choice_twelve, choice_thirteen, choice_fourteen, choice_fifteen,choice_sixteen) 
if(a == 16): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    six = raw_input('Enter the sixth element.\n') 
    seven = raw_input('Enter the seventh element.\n') 
    eight = raw_input('Enter the eighth element.\n') 
    nine = raw_input('Enter the ninth element.\n') 
    ten = raw_input('Enter the tenth element.\n') 
    eleven = raw_input('Enter the eleventh element.\n') 
    twelve = raw_input('Enter the twelth element.\n') 
    thirteen = raw_input('Enter the thirteenth element.\n') 
    fourteen = raw_input('Enter the fourteenth element.\n') 
    fifteen = raw_input('Enter the fifteenth element.\n') 
    sixteen = raw_input('Enter the sixteenth element.\n') 
    list_one = [one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    choice_seven = random.choice(list_one) 
    choice_eight = random.choice(list_one) 
    choice_nine = random.choice(list_one) 
    choice_ten = random.choice(list_one) 
    choice_eleven = random.choice(list_one) 
    choice_twelve = random.choice(list_one) 
    choice_thirteen = random.choice(list_one) 
    choice_fourteen = random.choice(list_one) 
    choice_fifteen = random.choice(list_one) 
    choice_sixteen = random.choice(list_one) 
    choice_seventeen = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six, choice_seven, choice_eight, choice_nine, choice_ten, choice_eleven, choice_twelve, choice_thirteen, choice_fourteen, choice_fifteen,choice_sixteen, choice_seventeen) 
if(a == 17): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    six = raw_input('Enter the sixth element.\n') 
    seven = raw_input('Enter the seventh element.\n') 
    eight = raw_input('Enter the eighth element.\n') 
    nine = raw_input('Enter the ninth element.\n') 
    ten = raw_input('Enter the tenth element.\n') 
    eleven = raw_input('Enter the eleventh element.\n') 
    twelve = raw_input('Enter the twelth element.\n') 
    thirteen = raw_input('Enter the thirteenth element.\n') 
    fourteen = raw_input('Enter the fourteenth element.\n') 
    fifteen = raw_input('Enter the fifteenth element.\n') 
    sixteen = raw_input('Enter the sixteenth element.\n') 
    seventeen = raw_input('Enter the seventeenth element.\n') 
    list_one = [one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    choice_seven = random.choice(list_one) 
    choice_eight = random.choice(list_one) 
    choice_nine = random.choice(list_one) 
    choice_ten = random.choice(list_one) 
    choice_eleven = random.choice(list_one) 
    choice_twelve = random.choice(list_one) 
    choice_thirteen = random.choice(list_one) 
    choice_fourteen = random.choice(list_one) 
    choice_fifteen = random.choice(list_one) 
    choice_sixteen = random.choice(list_one) 
    choice_seventeen = random.choice(list_one) 
    choice_eighteen = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six, choice_seven, choice_eight, choice_nine, choice_ten, choice_eleven, choice_twelve, choice_thirteen, choice_fourteen, choice_fifteen,choice_sixteen, choice_seventeen, choice_eighteen) 
if(a == 18): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    six = raw_input('Enter the sixth element.\n') 
    seven = raw_input('Enter the seventh element.\n') 
    eight = raw_input('Enter the eighth element.\n') 
    nine = raw_input('Enter the ninth element.\n') 
    ten = raw_input('Enter the tenth element.\n') 
    eleven = raw_input('Enter the eleventh element.\n') 
    twelve = raw_input('Enter the twelth element.\n') 
    thirteen = raw_input('Enter the thirteenth element.\n') 
    fourteen = raw_input('Enter the fourteenth element.\n') 
    fifteen = raw_input('Enter the fifteenth element.\n') 
    sixteen = raw_input('Enter the sixteenth element.\n') 
    seventeen = raw_input('Enter the seventeenth element.\n') 
    eighteen = raw_input('Enter the eighteenth element.\n') 
    list_one = [one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    choice_seven = random.choice(list_one) 
    choice_eight = random.choice(list_one) 
    choice_nine = random.choice(list_one) 
    choice_ten = random.choice(list_one) 
    choice_eleven = random.choice(list_one) 
    choice_twelve = random.choice(list_one) 
    choice_thirteen = random.choice(list_one) 
    choice_fourteen = random.choice(list_one) 
    choice_fifteen = random.choice(list_one) 
    choice_sixteen = random.choice(list_one) 
    choice_seventeen = random.choice(list_one) 
    choice_eighteen = random.choice(list_one) 
    choice_nineteen = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six, choice_seven, choice_eight, choice_nine, choice_ten, choice_eleven, choice_twelve, choice_thirteen, choice_fourteen, choice_fifteen,choice_sixteen, choice_seventeen, choice_eighteen, choice_nineteen) 
if(a == 19): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    six = raw_input('Enter the sixth element.\n') 
    seven = raw_input('Enter the seventh element.\n') 
    eight = raw_input('Enter the eighth element.\n') 
    nine = raw_input('Enter the ninth element.\n') 
    ten = raw_input('Enter the tenth element.\n') 
    eleven = raw_input('Enter the eleventh element.\n') 
    twelve = raw_input('Enter the twelth element.\n') 
    thirteen = raw_input('Enter the thirteenth element.\n') 
    fourteen = raw_input('Enter the fourteenth element.\n') 
    fifteen = raw_input('Enter the fifteenth element.\n') 
    sixteen = raw_input('Enter the sixteenth element.\n') 
    seventeen = raw_input('Enter the seventeenth element.\n') 
    eighteen = raw_input('Enter the eighteenth element.\n') 
    nineteen = raw_input('Enter the ninteenth element.\n') 
    list_one = [one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    choice_seven = random.choice(list_one) 
    choice_eight = random.choice(list_one) 
    choice_nine = random.choice(list_one) 
    choice_ten = random.choice(list_one) 
    choice_eleven = random.choice(list_one) 
    choice_twelve = random.choice(list_one) 
    choice_thirteen = random.choice(list_one) 
    choice_fourteen = random.choice(list_one) 
    choice_fifteen = random.choice(list_one) 
    choice_sixteen = random.choice(list_one) 
    choice_seventeen = random.choice(list_one) 
    choice_eighteen = random.choice(list_one) 
    choice_nineteen = random.choice(list_one) 
    choice_twenty = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six, choice_seven, choice_eight, choice_nine, choice_ten, choice_eleven, choice_twelve, choice_thirteen, choice_fourteen, choice_fifteen,choice_sixteen, choice_seventeen, choice_eighteen, choice_nineteen, choice_twenty) 
if(a == 20): 
    one = raw_input('Enter the first element.\n') 
    two = raw_input('Enter the second element.\n') 
    three = raw_input('Enter the third element.\n') 
    four = raw_input('Enter the fourth element.\n') 
    five = raw_input('Enter the fifth element.\n') 
    six = raw_input('Enter the sixth element.\n') 
    seven = raw_input('Enter the seventh element.\n') 
    eight = raw_input('Enter the eighth element.\n') 
    nine = raw_input('Enter the ninth element.\n') 
    ten = raw_input('Enter the tenth element.\n') 
    eleven = raw_input('Enter the eleventh element.\n') 
    twelve = raw_input('Enter the twelth element.\n') 
    thirteen = raw_input('Enter the thirteenth element.\n') 
    fourteen = raw_input('Enter the fourteenth element.\n') 
    fifteen = raw_input('Enter the fifteenth element.\n') 
    sixteen = raw_input('Enter the sixteenth element.\n') 
    seventeen = raw_input('Enter the seventeenth element.\n') 
    eighteen = raw_input('Enter the eighteenth element.\n') 
    nineteen = raw_input('Enter the ninteenth element.\n') 
    twenty = raw_input('Enter the twentieth element.\n') 
    list_one = [one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen,fifteen,sixteen,seventeen,eighteen,nineteen,twenty] 
    choice_one = random.choice(list_one) 
    choice_two = random.choice(list_one) 
    choice_three = random.choice(list_one) 
    choice_four = random.choice(list_one) 
    choice_five = random.choice(list_one) 
    choice_six = random.choice(list_one) 
    choice_seven = random.choice(list_one) 
    choice_eight = random.choice(list_one) 
    choice_nine = random.choice(list_one) 
    choice_ten = random.choice(list_one) 
    choice_eleven = random.choice(list_one) 
    choice_twelve = random.choice(list_one) 
    choice_thirteen = random.choice(list_one) 
    choice_fourteen = random.choice(list_one) 
    choice_fifteen = random.choice(list_one) 
    choice_sixteen = random.choice(list_one) 
    choice_seventeen = random.choice(list_one) 
    choice_eighteen = random.choice(list_one) 
    choice_nineteen = random.choice(list_one) 
    choice_twenty = random.choice(list_one) 
    choice_twenty_one = random.choice(list_one) 
    print(choice_one, choice_two, choice_three, choice_four, choice_five, choice_six, choice_seven, choice_eight, choice_nine, choice_ten, choice_eleven, choice_twelve, choice_thirteen, choice_fourteen, choice_fifteen,choice_sixteen, choice_seventeen, choice_eighteen, choice_nineteen, choice_twenty, choice_twenty_one) 
raw_input('Press enter to exit.') 

答えて

4

はい!簡単な方法があります。番号がnの場合は、nアクションを実行するので、ループに単純化できます。

import random 

n = int(raw_input('Number: ')) 
items = raw_input('Items: ').split() 

choices = [random.choice(items) for _ in xrange(n)] 
print choices 

すべてのアイテムを1つのスペースに区切り文字として入力できるようにしましたが、変更できるものです。あなたはそれを実行すると、あなたは多くの質問に見るように、あなたは、

Number: 5 
Items: '1 4 6 56 45 34 7 12 fgtr hyt 98 0 98 78 6666666666.6' 
['56', '98', "6666666666.6'", '7', 'fgtr'] 

を取得し、入力を信頼すると、ユーザーはあなたを与えるだろう賢明ではないので、try文に番号の入力をラップし、整数を取得するまで、必要な回数だけ繰り返します。

もう1つのことは、リストに数値と同じ数の項目を含める場合は、別のループを使用することができます。例えば

items = [raw_input('Input item number {}'.format(i+1)) for i in xrange(n)] 
+0

ありがとうございましたとても!今では、599だけを取る何かをするのに499行は必要ありません。 – Mathime

+0

@Mathimeあなたがそれを見なかった場合には、答えを編集して、ユーザーにn個の項目だけを入力するように求める方法を示しました。一般的な経験則として、たくさんのアクションを繰り返しているのであれば、おそらくループを使用する方法があります。そして、同じコードを多くの場所で複製している場合は、おそらくこれを関数に入れて1行で呼び出すことができます。 – Reti43

0

印刷ramdomになりまし
import random 
a = input('Enter a number of elements(maximum twenty).\n') 

if a < 2 and a > 20: 
    elements = [] 
    elementsramdon = [] 
    for x in range(a): 
     uservalue = raw_input('Enter the element.\n') 
     elements.append(uservalue) # this line contain the real values 
     elementsramdom.append(random.choice(uservalue)) # ramdon values 

for x in elementsramdom: 
    print x 

値か、カンマ個別の[X、Y、Z]で必要な場合

print ([x for x in elementsrandom]) 
関連する問題