改进Python代码

2024-04-18 13:10:17 发布

您现在位置:Python中文网/ 问答频道 /正文

这个代码是有效的,但效率不高。你能告诉我怎样使它更有效吗?我相信有那么多的功能,但我已经忘记了如何。剧本是一个多选测验。我将每个问题定义为一个新函数。这是最好的方法吗?你知道吗

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

Tags: theanswernameyounumberinputifis
1条回答
网友
1楼 · 发布于 2024-04-18 13:10:17

这只是一个指针。你知道吗

而不是

def sixq():
    global q6list, answer6
    ...

创建函数

def question(qlist, qanswer):
    ...    

并传入qlistqanswer作为参数,大部分代码是重复的,可以通过这种方式消除。同时您还消除了global的使用。您可以使用return将所需的任何值返回到此函数末尾的调用代码。(注意Python允许您返回多个值)

根据需要调整上述参数,即如果需要提供另一个参数等。基本上,您希望将重复的代码分解成一个单个函数,并提供使其唯一的必要参数。你知道吗

最后,用一个函数代替八个函数,将使维护代码变得更容易。如果您发现代码有问题,或者只是想更改某些内容,您只需在一个地方进行更正,而不是在8个不同的函数中进行更正。。这是一大好处。你知道吗

相关问题 更多 >