改进代码-Python

改进代码-Python,python,Python,此代码可以工作,但效率不尽可能高。你能告诉我怎样使它更有效吗?我相信我们没有那么多的功能,但我已经忘记了怎么做。脚本是一个多选测验。我将每个问题定义为一个新函数。这是最好的方法吗 def firstq (): # 1st Question global q1list,answer1 q1list = ["Wellington","Auckland","Motueka","Masterton"] q1count = 0 print ("Question 1")

此代码可以工作,但效率不尽可能高。你能告诉我怎样使它更有效吗?我相信我们没有那么多的功能,但我已经忘记了怎么做。脚本是一个多选测验。我将每个问题定义为一个新函数。这是最好的方法吗

def firstq (): # 1st Question
    global q1list,answer1
    q1list = ["Wellington","Auckland","Motueka","Masterton"]
    q1count = 0
    print ("Question 1")
    print ("From which of the following Towns is the suburb NEWLANDS located?")
    while q1count < 4:
        print (q1count,"  ",q1list[q1count])
        q1count = q1count + 1
    answer1 = int(input("What number answer do you choose?"))
    if answer1 == 0: print ("Correct answer!")
    elif answer1 != 0: print ("Sorry! Incorrect Answer. Better luck with the next Question.")
    print("Next Question below:")
    print(" ")
    print(" ")



def secq (): #Second Question
    global q2list,answer2 # Makes answer2 and q2list avalible anywhere on the page.
    q2list = ["Wellington","Christchurch","Pairoa","Dunedin"] # List of answers to choose from.
    q2count = 0 # defines what the q2 count is. SEE BELOW
    print ("Question 2")# prints "question 2"
    print ("What NZ town is known for L&P?") # Prints the question
    while q2count < 4:
        print (q2count,"  ",q2list[q2count]) # Whilst the number of answers (q2list) is below 4 it will print the next answer.
        q2count = q2count + 1
    answer2 = int(input("What number answer do you choose?")) # asks for answer
    if answer2 == 2: print ("Correct answer!") # If answer is correct, prints "Correct answer"
    elif answer2 != 2: print ("Sorry! Incorrect Answer. Better luck with the next Question.") # If answer is correct, prints "Sorry! Incorrect Answer. Better luck with the next Question."
    print("Next Question below:") # prints "Next Question
    # these provide spacing!
    print(" ")
    print(" ")


def thrq ():
    global q3list,answer3
    q3list = ["Lewis Carroll","J.K. Rowling","Louis Carroll","Other"]
    q3count = 0
    print ("Question 3")
    print ("Who wrote the book Alice In Wonderland?")
    while q3count < 4:
        print (q3count,"  ",q3list[q3count])
        q3count = q3count + 1
    answer3 = int(input("What number answer do you choose?"))
    if answer3 == 0: print ("Correct answer!")
    elif answer3 != 0: print ("Sorry! Incorrect Answer. Better luck with the next Question.")
    print("Next Question below:")
    print(" ")
    print(" ")

def fouq ():
    global q4list,answer4
    q4list = ["WA","DC","WD","WC"]
    q4count = 0
    print ("Question 4")
    print ("What is the abbreviation for Washington?")
    while q4count < 4:
        print (q4count,"  ",q4list[q4count])
        q4count = q4count + 1
    answer4 = int(input("What number answer do you choose?"))    
    if answer4 == 1: print ("Correct answer!")
    elif answer4 != 1: print ("Sorry! Incorrect Answer. Better luck with the next Question.")
    print("Next Question below:")
    print(" ")
    print(" ")

def fivq ():
    global q5list,answer5
    q5list = ["Yes","No, they're found around New Zealand","No","No, they're found around the UK"]
    q5count = 0
    print ("Question 5")
    print ("Are walruses found in the South Pole?")
    while q5count < 4:
        print (q5count,"  ",q5list[q5count])
        q5count = q5count + 1
    answer5 = int(input("What number answer do you choose?"))
    if answer5 == 2: print ("Correct answer!")
    elif answer5 != 2: print ("Sorry! Incorrect Answer. Better luck with the next Question.")
    print("Next Question below:")
    print(" ")
    print(" ")

def sixq ():
    global q6list,answer6
    q6list = ["G.M.","General M's","G Motors","Grand Motors"]
    q6count = 0
    print ("Question 6")
    print ("What is the other name for General Motors?")
    while q6count < 4:
        print (q6count,"  ",q6list[q6count])
        q6count = q6count + 1
    answer6 = int(input("What number answer do you choose?"))    
    if answer6 == 0: print ("Correct answer!")
    elif answer6 != 0: print ("Sorry! Incorrect Answer. Better luck with the next Question.")
    print("Next Question below:")
    print(" ")
    print(" ")

def sevq ():
    global q7list,answer7
    q7list = ["Greece","USA","Egypt","Italy"]
    q7count = 0
    print ("Question 7")
    print ("Which of the following countries were cats most honored in?")
    while q7count < 4:
        print (q7count,"  ",q7list[q7count])
        q7count = q7count + 1
    answer7 = int(input("What number answer do you choose?"))
    if answer7 == 2: print ("Correct answer!")
    elif answer7 != 2: print ("Sorry! Incorrect Answer. Better luck with the next Question.")
    print("Next Question below:")
    print(" ")
    print(" ")

def eigq ():
    global q8list,answer8
    q8list = ["I find","I see","I presume","I am"]
    q8count = 0
    print ("Question 8")
    print ("Complete this phrase-Dr. Livingstone,")
    while q8count < 4:
        print (q8count,"  ",q8list[q8count])
        q8count = q8count + 1
    answer8 = int(input("What number answer do you choose?"))       
    if answer8 == 2: print ("Correct answer!")
    elif answer8 != 2: print ("Sorry! Incorrect Answer. Better luck with the next Question.")

    print(" ")
    print(" ")

def end():
    if answer1 == 0 and answer2 == 2 and answer3 == 0 and answer4 ==1 and answer5 ==2 and answer6 ==0 and answer7 == 2 and answer8 == 2: print("YAY, all questions correct! You have won the 1 million!")
    else: print("Sorry you have some incorrect questions! You have not won any money! :(") # If all answers are correct, this will display YAY, all questions correct! You have won the 1 million! If not it will print Sorry you have some incorrect questions! You have not won any money! :(.


def printorder ():
    # Defines ther order that it will be printed in
    firstq()
    secq()
    thrq()
    fouq()
    fivq()
    sixq()
    sevq()
    eigq()
    end()

name = l" " # while name is blank it will continue
while name != "quit": #While the name is not quit it will continue. If name is quit it will stop.
    print ("The $1,000,000 Quiz! Can you win the 1 Million?")#Prints Welcome Message
    name = input("Lets Get Started! What is your name: ")# asks for name
    if name == "quit": 
       break # if the name is quit it will stop if not....
    printorder()# ....prints printorder
def firstq():#第一个问题
全球问题清单,答案1
q1list=[“惠灵顿”、“奥克兰”、“莫图埃卡”、“马斯顿”]
q1count=0
打印(“问题1”)
打印(“纽兰兹郊区位于以下哪个城镇?”)
当Q1计数小于4时:
打印(q1count,“,q1list[q1count])
q1count=q1count+1
answer1=int(输入(“您选择的答案是多少?”)
如果答案1==0:打印(“正确答案!”)
艾利夫回答1!=0:打印(“对不起!回答不正确。祝你下一个问题好运。”)
打印(“下面的下一个问题:”)
打印(“”)
打印(“”)
def secq():#第二个问题
全局q2list、answer2#使answer2和q2list在页面上的任何位置都可用。
q2list=[“惠灵顿”、“基督城”、“佩罗亚”、“达尼丁”]#可供选择的答案列表。
q2count=0#定义了q2计数。见下文
打印(“问题2”)#打印“问题2”
打印(“哪个新西兰小镇以L&P闻名?”)#打印问题
当Q2计数小于4时:
打印(q2count,“,q2list[q2count])#当答案数(q2list)低于4时,将打印下一个答案。
q2count=q2count+1
answer2=int(输入(“您选择的答案是多少?”)#询问答案
如果答案2==2:打印(“正确答案!”)#如果答案正确,打印“正确答案”
艾利夫回答2!=2:打印(“对不起!回答不正确。祝你下一个问题好运。”)#如果答案正确,打印“对不起!回答不正确。祝你下一个问题好运。”
打印(“下一个问题:)#打印”下一个问题
#这些提供了间隔!
打印(“”)
打印(“”)
def thrq():
全球q3list,answer3
q3list=[“刘易斯·卡罗尔”、“J.K.罗琳”、“路易斯·卡罗尔”、“其他”]
q3count=0
打印(“问题3”)
印刷(“谁写了《爱丽丝梦游仙境》?)
当Q3计数小于4时:
打印(q3count,“,q3list[q3count])
q3count=q3count+1
answer3=int(输入(“您选择的答案是多少?”)
如果答案3==0:打印(“正确答案!”)
elif answer3!=0:print(“对不起!回答不正确。祝你下一个问题好运。”)
打印(“下面的下一个问题:”)
打印(“”)
打印(“”)
def fouq():
全球q4list,answer4
q4list=[“华盛顿”、“华盛顿”、“华盛顿”、“华盛顿”]
q4count=0
打印(“问题4”)
打印(“华盛顿的缩写是什么?”)
当q4count<4时:
打印(q4count,“,q4list[q4count])
q4count=q4count+1
answer4=int(输入(“您选择的答案是什么数字?”)
如果答案4==1:打印(“正确答案!”)
elif answer4!=1:print(“对不起!回答不正确。祝你下一个问题好运。”)
打印(“下面的下一个问题:”)
打印(“”)
打印(“”)
def fivq():
全球问题5清单,答案5
q5list=[“是”、“不是,它们在新西兰各地都有”、“不是”、“不是,它们在英国各地都有”]
Q5计数=0
打印(“问题5”)
打印(“南极有海象吗?”)
当Q5计数小于4时:
打印(q5count,“,q5list[q5count])
q5count=q5count+1
answer5=int(输入(“您选择的答案是多少?”)
如果答案5==2:打印(“正确答案!”)
elif answer5!=2:print(“对不起!回答不正确。祝你下一个问题好运。”)
打印(“下面的下一个问题:”)
打印(“”)
打印(“”)
def sixq():
全球Q6列表,答案6
q6list=[“通用汽车”、“通用汽车”、“通用汽车”、“大汽车”]
q6count=0
打印(“问题6”)
打印(“通用汽车的其他名称是什么?”)
当Q6计数小于4时:
打印(q6count,“,q6list[q6count])
q6count=q6count+1
answer6=int(输入(“您选择的答案是什么数字?”)
如果答案6==0:打印(“正确答案!”)
elif answer6!=0:print(“对不起!回答不正确。祝你下一个问题好运。”)
打印(“下面的下一个问题:”)
打印(“”)
打印(“”)
def sevq():
全球问题7清单,答案7
q7list=[“希腊”、“美国”、“埃及”、“意大利”]
q7count=0
打印(“问题7”)
打印(“以下哪个国家的猫最受尊敬?”)
当Q7计数小于4时:
打印(q7count,“,q7list[q7count])
Q7计数=Q7计数+1
answer7=int(输入(“您选择的答案是多少?”)
如果答案7==2:打印(“正确答案!”)
elif answer7!=2:print(“对不起!回答不正确。祝你下一个问题好运。”)
打印(“下面的下一个问题:”)
打印(“”)
打印(“”)
defeigq():
全球问题8清单,答案8
q8list=[“我发现”、“我看到”、“我假设”、“我是”]
q8count=0
打印(“问题8”)
打印(“填写此短语-Livingstone博士,”)
当Q8计数小于4时:
打印(q8count,“,q8list[q8count])
q8count=q8count+1
answer8=int(输入(“您选择的答案是什么数字?”)
如果答案8==2:打印(“正确答案!”)
elif answer8!=2:print(“对不起!回答不正确。祝你下一个问题好运。”)
打印(“”)
打印(“”)
def end():
如果回答1==0,回答2==2,回答3==0,回答4==1,回答5==2,回答6==0,回答7==2,回答8==2:打印(“耶,所有问题都正确!你赢得了100万!”
其他:打印(“对不起,你有一些不正确的问题!你没有赢得任何钱!”:(“”)如果所有答案都正确,这将显示耶,所有问题都正确!你赢得了100万!如果没有,它将打印对不起,你有一些不正确的问题!你没有赢得任何钱!:()。
def printorder():
#定义打印顺序
firstq()
secq()
thrq()
fouq()
fivq()
sixq()
sevq()
eigq()
def sixq():
    global q6list, answer6
    ...
def question(qlist, qanswer):
    ...