关于random.shuffle,数组

时间:2016-10-16 06:50:59

标签: python random

大家好,我有这个代码。它运作良好(也教我很多关于Python,因为我是新手,有点初学者,但很好地追赶。)无论如何,我想在下面的代码中改变问题。我理解random.shuffle和数组的想法。只是不确定如何把它放在一起,什么最适合

def game():                                                                                         #defines the games script
    start_time = time.time()                                                                        #starts game timer.
    correct = 0                                                                                     #recalls amount of correct questions.
    wrong = 0
    time.sleep(2)                                                                                  #sleep function pauses game for 2 seconds.
    list1 = ["10","7","13","4","1","6","9","12","17","2"]                                           #list of answers, if statements check answer for each question. i.e: 1 number = answer for 1 question.

    print "\nQuestion 1:\n"                                                                         #printed text.
    print "First question: _ - 8 = 2"                                                               #prints user question.
    print "a, 10"                                                                                   #prints question hint.
    print "b, 9"                                                                                    #prints question hint.
    print "c, 13"                                                                                   #prints question hint.
    list1 = raw_input("Your answer:  ")                                                             #asks user to input answer.
    if list1 == "10":                                                                               #checks list above for answer.
        correct += 1                                                                                #adds 1 to correct total.
        print "\n\t  Wonderful, correct",list1,"- 8 = 2\n"                                          #printed text.
    else:                                                                                           #if not correct, else checks and chooses wrong.
        wrong +=1                                                                                   #adds 1 to wrong total.
        print "\n\tSorry you got that number wrong. The correct number was 10\n"                      #printed text.

2 个答案:

答案 0 :(得分:0)

只需传入列表即可使用random.shuffle。

import random

list1 = ["10","7","13","4","1","6","9","12","17","2"]
random.shuffle(list1) # list1 is now shuffled
print list1 # observe shuffled list

答案 1 :(得分:0)

def game():                                                                                         #defines the games script
    start_time = time.time()                                                                        #starts game timer.
    correct = 0                                                                                     #recalls amount of correct questions.
    wrong = 0
    time.sleep(2)                                                                                  #sleep function pauses game for 2 seconds.
    list2 = ["\nQuestion 1", "\nQuestion 2", "\nQuestion 3", "\nQuestion 4", "\nQuestion 5", "\nQuestion 6", "\nQuestion 7", "\nQuestion 8", "\nQuestion 9", "\nQuestion 10",]
    list3 = ["_ - 8 = 2", "_ - 4 = 3", "20 - _ = 7", "11 - 7 = _", "5 - _ = 4", "19 - 13 = _", "_ - 7 = 2", "17 - 5 = _", "_ - 6 = 11", "15 - 13 = _"]
    ans = ["10","7","13","4","1","6","9","12","17","2"]                                           #list of answers, if statements check answer for each question. i.e: 1 number = answer for 1 question.
    ans2 = ["10 - 8 = 2", "7 - 4 = 3", "20 - 13 = 7", "11 - 7 = 4", "5 - 1 = 4", "19 - 13 = 6", "9 - 7 = 2", "17 - 5 = 12", "17 - 6 = 11", "15 - 13 = 2"]
    op = list(zip(list3, ans, ans2))                                                              #zips operations to be randomly unzipped.
    random.shuffle(op)                                                                      #shuffles the questions out of order everytime
    list3, ans, ans2 = zip(*op)                                                                   #sets op as zip * is used to unzip

    print list2[0]                                                                        #printed text.
    print list3[0]                                                               #prints user question.
    op = raw_input("Your answer:  ")                                                             #asks user to input answer.
    if op == ans[0]:                                                                               #checks list above for answer.
        correct += 1                                                                                #adds 1 to correct total.
        print "\n\t  Wonderful, correct",ans2[0],"\n"                                      #printed text.
    else:                                                                                           #if not correct, else checks and chooses wrong.
        wrong +=1                                                                                   #adds 1 to wrong total.
        print "\n\tSorry you got that number wrong.",ans2[0],"\n"                      #printed text.