大规模的Python随机共享者

时间:2016-03-26 14:15:01

标签: python python-2.7 random

我尝试制作一个程序来获取一定数量的元素并随机生成它们。例如,用户可以输入20个名称并使算法随机吐出。我以这样一种方式构建程序,你最多可以输入20个元素,它会打印出这个数字加上一个随机的东西。所以你可以输入二十个元素,它会随机选择二十一个元素。问题是我不是太经验随机,所以我做得非常大(只有500行)。它有效,但我想: 1)缩短它。 2)取任何数字,而不是2到20和3)能够吐出用户输入的随机项目数。我不知道是否有任何意义,但如果可以,请帮忙! 整个海量代码:

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.')

2 个答案:

答案 0 :(得分: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

我已经做到了,所以你可以一次性输入所有项目,空格作为分隔符,但这是你可以改变的。当你运行它时,你得到

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语句中包含数字的输入并根据需要重复多次,直到获得整数。

while True:
    try:
        n = int(raw_input('Number: '))
        break
    except ValueError:
        print 'Gimme an integer please!'

另一件事,如果你希望你的列表包含与数字一样多的项目,你可以使用另一个循环。

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

答案 1 :(得分:0)

例如

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

现在用于打印ramdom值

for x in elementsramdom:
    print x

或者如果你想用逗号分隔[x,y,z]

print ([x for x in elementsrandom])