我正在尝试对Python进行测验,但我的代码不断重复

时间:2018-01-25 14:49:38

标签: python python-3.x

所以,我正在尝试在Python中进行基本测验,当我在测试程序时遇到某个问题时,我的代码会重复,原因似乎无法解决...

我的代码:

`if choice ==“2”:#如果用户输入2,他们将被带到这里。

        print (" ") #Makes a break between lines.
        username=input ("Please enter your username:")

        print (" ") #Makes a break between lines.
        password=input ("Please enter your password:")

        file=open ("userinformation.txt","r")

        for line in file:

                if username and password in line:


                    print (" ") #Makes a break between lines.
                    print ("Thank you for logging in " + username+".")
                    time.sleep (3)

                    #file.close()


                    print (" ") #Makes a break between lines.
                    print ("Please choose a topic and a difficulty for your quiz.")
                    time.sleep (4)

                    print (" ") #Makes a break between lines.
                    print("a.History - Easy.") 

                    print (" ") #Makes a break between lines.
                    print("b.History - Medium.") 

                    print (" ") #Makes a break between lines.
                    print("c.History - Hard.") 

                    print (" ") #Makes a break between lines.
                    print("d.Music - Easy.")

                    print (" ") #Makes a break between lines.
                    print("e.Music - Medium.")

                    print (" ") #Makes a break between lines.
                    print("f.Music - Hard.")

                    print (" ") #Makes a break between lines.
                    print("g.Computer Science - Easy.")

                    print (" ") #Makes a break between lines.
                    print("h.Computer Science - Medium.")

                    print (" ") #Makes a break between lines.
                    print("i.Computer Science - Hard.")

                    print (" ") #Makes a break between lines. 
                    topic = input("To choose a topic please enter the corrosponding letter:")

                    if topic == "a":
                        print (" ") #Makes a break between lines.
                        time.sleep(2)
                        print ("You have selected option a, History - Easy.") #Tells the user what subject they picked (Same result but with a different topic and difficulty displayed for each one

                        print (" ")
                        print ("Rules: You have an unlimited amount of time to anwser each question. You will be anwsering 2 questions and each question anwsered correctly")
                        print ("will reward you with 10 points.")
                        time.sleep(9)

                        print (" ")
                        print ("The maximum amount of points you can get is 20.")
                        time.sleep(3)

                        print (" ")
                        print ("Good luck!")
                        print (" ")
                        time.sleep(2)

                        print ("Question 1 -")
                        print ("When did World War 1 begin and end?")


                        print ("a. 1913 - 1917") #Incorrect anwser
                        print (" ")
                        print ("b. 1914 - 1919") #Incorrect anwser
                        print (" ")
                        print ("c. 1914 - 1918") #Correct anwser
                        anwserq1he = input ("Please pick an anwser:")

                        if anwserq1he == "b":
                            print(" ")
                            print("Sorry, that's the wrong anwser...")
                            time.sleep(3)
                            print(" ")
                            hisready = input ("Type ok when you are ready to proceed to the next question.")

                        elif anwserq1he == "a":
                            print(" ")
                            print("Sorry, that's the wrong anwser...")
                            time.sleep(3)
                            print(" ")
                            hisready = input ("Type ok when you are ready to proceed to the next question.")



                        elif anwserq1he == "c":
                            print(" ")
                            print ("That's the right anwser!")

                            print(" ")
                            time.sleep(2)
                            print ("Adding 10 points to your score...")

                            score = score +10

                            print(" ")
                            time.sleep(3)
                            hisready = input ("Type ok when you are ready to proceed to the next question.")



                            if hisready == "ok":
                                print(" ")
                                time.sleep(2)
                                print ("Question 2-")
                                print ("Which historical figure is commonly known as 'The lady with the lamp'?")
                                print ("a. Margaret Fuller")
                                print (" ")
                                print ("b. Florence Nightingale")
                                print (" ")
                                print ("c. Rosa Luxemburg")
                                anwserq2he = input ("Please pick an anwser:")

                                if anwserq2he == "a":
                                    print ("Sorry, that's the wrong anwser...")

                                    results = input("Please type results to get your results")

                                elif anwserq2he == "c":
                                    print ("Sorry, that's the wrong anwser...")

                                    results = input("Please type results to get your results")



                                elif anwserq2he == "b":
                                    print (" ")
                                    time.sleep(2)
                                    print ("That's the right anwser!")

                                    print(" ")
                                    time.sleep(2)
                                    print ("Adding 10 points to your score...")

                                    score = score + 10

                                    results = input("Please type results to get your results.")

                                    if results == "results":

                                        print(" ")
                                        time.sleep(3)
                                        print ("Getting your results...")

                                        if score == 20:
                                            print ("Congratulations, you scored 20 points. That's the best score you can get!")

                                        elif score == 10:
                                                   print ("Well done, you scored 10 points. Almost there!")

                                        elif score == 0:
                                                   print ("You scored 0 points. Try again and you might do better!")`

当我完成测验时,print ("Thank you for logging in " + username+".")以外的所有内容都会重复...如果有人能帮助我,我会很感激。谢谢。

旁注:#file.close是故意的。

1 个答案:

答案 0 :(得分:0)

好吧,首先我们需要改进程序布局,也许在某些函数中调用会很有用。以下是修复程序的一些提示。

首先使用:如果username.strip()和password.strip()在行中:[使用.strip()函数,它将匹配一个单词。

您的程序效率低下,因为如果按2中的其他值按下它将会破坏程序。你想循环它并反复循环它,因此使用while True语句并创建一个函数。

添加一个名为exampleFunction()(I.E)的函数,然后在代码广告的底部这行,但显然缩进它。

while True:

     exampleFunction()

你不需要使用或只是做文件=打开(“你的文件.txt”)

例如,

genre=input("What is the film  you would like to search?: ")
f=open("Films.txt")
for line in f:
    if genre in line.strip():
        print(line)

我个人会自己解决这个问题并发布一份代码副本但是这个代码太小而无法解决,因此我将其留给您。

古德勒克,

马丁