首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关于random.shuffle,数组

关于random.shuffle,数组
EN

Stack Overflow用户
提问于 2016-10-16 14:50:59
回答 2查看 119关注 0票数 1

嗨,我有这段代码。它工作得很好(也教会了我很多关于Python的知识,因为我是新手,有点初学者,但跟得上)。无论如何,我想在下面的代码中打乱问题。我理解random.shuffle和数组的概念。只是不知道如何将它们组合在一起,以及什么最适合

代码语言:javascript
复制
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.
EN

回答 2

Stack Overflow用户

发布于 2016-10-16 15:01:40

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

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2016-10-16 21:06:25

代码语言:javascript
复制
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.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40067692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档