如何回到测验中while循环的开始

时间:2018-07-12 22:12:38

标签: python loops for-loop while-loop

这是我进行的测验,要求用户将阿拉伯语单词翻译成英语。当用户到达测验的结尾时,它询问用户是否要再次玩。如果用户输入单词,那么测验应该重新启动。我需要帮助,当用户说是时重新启动此循环。

下面列出了代码:

import time
x = 0
print("Welcome to the Arabic Vocabulary Test...")
print("Loading...")
time.sleep(1)
correct = 0

import random
words = ["بىت","مسجد","باب","كتب","قلم","مفت ح","مكتب","سرير","كرسي"]
while x <9:
    quest = random.choice(words)
    print("What is" , quest, "?" )
    words.remove(quest)
    ans = input()
    x = x+1
    if quest == "بىت" and ans == "a house":
        print("Correct")
        correct = correct + 1
    elif quest == "مسجد" and ans == "a mosque":
        print("Correct")
        correct = correct + 1
    elif quest == "باب" and ans == "a door":
        print("correct")
        correct = correct + 1
    elif quest == "كتب" and ans == "a book":
        print("correct")
        correct = correct + 1
    elif quest == "قلم" and ans == "a pen":
        print("correct")
        correct = correct + 1
    elif quest == "مفت ح" and ans == "a key":
        print("correct")
        correct = correct + 1
    elif quest == "مكتب" and ans == "a table":
        print("Correct")
        correct = correct + 1
    elif quest == "سرير" and ans == "a bed":
        print("correct")
        correct = correct + 1
    elif quest == "كرسي" and ans == "a chair":
        print("correct")
        correct = correct + 1
    else:
            print("Incorrect")


print("You got" ,correct , "out of 9.")
print("")
print("Check your answers")

print('''a house = "بىت"
a mosque = "مسجد"
a door = "باب"
a book = "كتب"
a pen = "قلم"
a key = "مفت ح"
a table = "مكتب"
a bed = "سرير"
a chair = "كرسي" 
''')
again = input("Would you like to play again?: ")

2 个答案:

答案 0 :(得分:0)

在while循环中包含所有测验代码。如果用户想再次玩,只需将您的计数器“ x”重置为0。

temp_words = words
while x <9:
    quest = random.choice(temp_words)
    print("What is" , quest, "?" )
    temp_words.remove(quest)
    ans = input()
    x = x+1
    if quest == "بىت" and ans == "a house":
        print("Correct")
        correct = correct + 1
    elif quest == "مسجد" and ans == "a mosque":
        print("Correct")
        correct = correct + 1
    elif quest == "باب" and ans == "a door":
        print("correct")
        correct = correct + 1
    elif quest == "كتب" and ans == "a book":
        print("correct")
        correct = correct + 1
    elif quest == "قلم" and ans == "a pen":
        print("correct")
        correct = correct + 1
    elif quest == "مفت ح" and ans == "a key":
        print("correct")
        correct = correct + 1
    elif quest == "مكتب" and ans == "a table":
        print("Correct")
        correct = correct + 1
    elif quest == "سرير" and ans == "a bed":
        print("correct")
        correct = correct + 1
    elif quest == "كرسي" and ans == "a chair":
        print("correct")
        correct = correct + 1
    else:
            print("Incorrect")


    print("You got" ,correct , "out of 9.")
    print("")
    print("Check your answers")

    print('''a house = "بىت"
    a mosque = "مسجد"
    a door = "باب"
    a book = "كتب"
    a pen = "قلم"
    a key = "مفت ح"
    a table = "مكتب"
    a bed = "سرير"
    a chair = "كرسي" 
    ''')
    again = input("Would you like to play again?: ")

    if again is 'yes':
        x = 0
        temp_words = words

答案 1 :(得分:0)

您可以将代码放入函数中,并在while循环中调用它:

def quiz():
    // the quiz code here

while True:
    quiz()
    again = input("Would you like to play again?: ")
    if again != "yes":
        # If the answer is not `yes`, stop the loop
        break

顺便说一句,关于您的测验代码的一些评论:)

  • 您可以使用词典来输入单词及其答案:

    words = {
        "بىت" : "a house",
        "مسجد" : "a mosque",
        ...
    }
    
  • 然后,编码测验的另一种方法是:

    def quiz():
        # Get the list of words
        questions = words.keys()
        # Shuffle the questions
        random.shuffle(questions)
        count = 0
        for quest in questions:
            ans = input()
            # Check the answer in the dict
            if ans == words[quest]:
                count += 1
                print("Correct")
            else:
                print("Incorrect")
    
        print("You got", count, "out of", len(questions), ".")
        print("")
        print("Check your answers")
        # Print the keys and values of the `words` dictionary
        for word, value in words.items():
            print(word, "=", value)
    

希望有帮助!