Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在我的测验中返回while循环的开头_Python_Loops_For Loop_While Loop - Fatal编程技术网

Python 如何在我的测验中返回while循环的开头

Python 如何在我的测验中返回while循环的开头,python,loops,for-loop,while-loop,Python,Loops,For Loop,While Loop,这是我做的一个测验,要求用户将阿拉伯语单词翻译成英语。当用户到达测验结束时,它会询问用户是否愿意再次玩。如果用户输入单词yes,则测验应重新启动。当用户回答yes时,我需要帮助使循环重新启动 代码如下所示: import time x = 0 print("Welcome to the Arabic Vocabulary Test...") print("Loading...") time.sleep(1) correct = 0 import random words = ["بىت","م

这是我做的一个测验,要求用户将阿拉伯语单词翻译成英语。当用户到达测验结束时,它会询问用户是否愿意再次玩。如果用户输入单词yes,则测验应重新启动。当用户回答yes时,我需要帮助使循环重新启动

代码如下所示:

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?: ")
导入时间
x=0
打印(“欢迎参加阿拉伯语词汇测试…”)
打印(“加载…”)
时间。睡眠(1)
正确=0
随机输入
单词=[“بتت㶤㶧”、“㶕㶖”、“㶤㶖”、“لم”、“م㶤”、“㶤㶖”、“㶤㶤”、“㶤”

whilex包括while循环中的所有测验代码。如果用户想再次播放,只需将计数器“x”重置为0即可

temp_words=单词

while x您可以将代码放入函数中,并在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)
    
希望有帮助

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)