我想做一个关于Python的测试,但是我的代码一直在重复

2024-04-27 03:41:05 发布

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

所以,我试着用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+".")之外的一切都会重复…如果有人能帮我,我将不胜感激。非常感谢。你知道吗

旁注:#文件.close是故意的。你知道吗


Tags: thetoyouinputyourtimesleepbetween
1条回答
网友
1楼 · 发布于 2024-04-27 03:41:05

好的,首先我们需要改进程序布局,也许调用一些函数会有用。下面是一些修复程序的技巧。你知道吗

首先使用:如果用户名.strip()和密码.strip()在第行:[使用.strip()函数,使其与单词匹配。你知道吗

你的程序效率低下,因为如果你按另一个值,如2,它会打破程序。您希望循环它并一遍又一遍地循环它,因此使用while True语句并创建一个函数。你知道吗

添加一个名为exampleFunction()(即)的函数,然后在代码的底部添加这一行,但显然要缩进它

while True:

     exampleFunction()

您不需要使用a或r just do file=open(“您的文件.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)

我会亲自修复这个自己和张贴的代码副本,但这是太小,所以我把它留给你。你知道吗

祝你好运

马汀

相关问题 更多 >