【发布时间】:2021-06-13 13:55:51
【问题描述】:
我想询问用户是否想使用 while 循环再次播放。这是我的代码。我该怎么做?如果我在另一个 while 之上堆叠另一个 while 循环,我会收到错误消息。我不确定我是否做得对。
QM = ["The average of first 50 natural numbers is _____\na. 25.30\nb. 25.5\nc. 25.00\nd. 12.25\nAnswer: ",
"A clock strikes once at 1 o’clock, twice at 2 o’clock, thrice at 3 o’clock and so on. How many times will it strike in 24 hours?\na. 78\nb. 136\nc. 156\nd. 196\nAnswer: ",
"106 × 106 – 94 × 94 = ?\na. 2004\nb. 2400\nc. 1904\nd. 1906\nAnswer: ",
"Which of the following numbers gives 240 when added to its own square?\na. 15\nb. 16\nc. 18\nd. 20\nAnswer: ",
"If David’s age is 27 years old in 2011. What was his age in 2003?\na. 17 years\nb. 37 years\nc. 20 years\nd. 19 years\nAnswer: ",
"What is 7% equal to?\na. 0.007\nb. 0.07\nc. 0.7\nd. 7\nAnswer: ",
"I am a number. I have 7 in the ones place. I am less than 80 but greater than 70. What is my number?\na. 71\nb. 73\nc. 75\nd. 77\nAnswer: ",
"How many years are there in a decade?\na. 5\nb. 10\nc. 15\nd. 20\nAnswer: ",
"What is the square of 15?\na. 15\nb. 30\nc. 252\nd. 225\nAnswer: ",
"In a century how many months are there?\na. 12\nb. 120\nc. 1200\nd. 12,000\nAnswer: ",
"If a number has an even number or zero at its unit place; the number is always divisible by ____\na. 2\nb. 5\nc. 3\nd. 7\nAnswer: ",
"An acute angle is ____\na. 90 degree.\nb. less than 90 degree.\nc. more than 90 degree.\nd. None of these.\nAnswer: ",
"What is the opposite of 6?\na. 6\nb. 5\nc. 4\nd. -6\nAnswer: ",
"Absolute value of -20 or |-20| is ________\na. 0\nb. -20\nc. |-20|\nd. 20\nAnswer: ",
"Which of these following set of numbers are factors of 24?\na. 2, 3, 4, 6, 8\nb. 1, 5, 12, 18\nc. 4, 7, 24\nd. 3, 9, 12\nAnswer: "
]
questionsEM = [
Question(QM[0], "b"),
Question(QM[1], "c"),
Question(QM[2], "b"),
Question(QM[3], "a"),
Question(QM[4], "d"),
Question(QM[5], "b"),
Question(QM[6], "d"),
Question(QM[7], "b"),
Question(QM[8], "d"),
Question(QM[9], "c"),
Question(QM[10], "a"),
Question(QM[11], "b"),
Question(QM[12], "d"),
Question(QM[13], "d"),
Question(QM[14], "a"),
]
print("=====QUIZ APPLICATION=====")
print ("=====INSTRUCTION====")
print ("""Every subject, they have their own difficulties. Every difficulties consists of 10 questions.
The difficulties are: Easy, Medium, and Hard.""")
print ("THE SUBJECTS FOR THE QUIZ ARE:")
如果用户想再次播放,我想从这里重新启动程序。
print("1. Math")
print ("2. English")
print ("3. Science")
while True:
subj = input("Choose a subject by typing the number: ")
subj = int(subj)
if subj > 3:
print("The number does not define the subject.")
continue
else:
if subj == 1:
print("===================================")
print("You have chosen the subject Math")
print("First difficulty will be EASY.")
print("Let's proceed to the quiz! GOOD LUCK!\n")
def run_EM(questionsEM):
score = 0
random.shuffle(questionsEM)
for question in questionsEM[:10]:
answer = input(question.prompt).lower()
if answer == question.answer:
score += 1
print("Correct")
else:
print("Wrong")
print("You got " + str(score) + "/" + str(10) + " correct")
print("Game Over! You did not passed the easy level")
询问用户是否想再玩一次
#again = str(input("Do you want to play again? [Y] / [N]: "))
#if again == "N" or "n":
break
if score == 10:
print("You got " + str(score) + "/" + str(10) + " correct")
print("==============================")
print("YOU MOVED ON FROM EASY LEVEL. LET'S GO FOR THE MEDIUM LEVEL. STARTING NOW.....")
print("==============================")
import QuestionMM
run_EM(questionsEM)
break
【问题讨论】:
-
能否提供完整的代码?很难理解。
-
编辑了代码。我只包括了数学问题和答案,因为我只在主题等于 1 时显示条件。
-
所以当用户再次输入 N 或 n 时,您试图退出 while 循环,对吗?
-
如果你在代码的开头声明一个变量
check = True,然后放上while check:。在代码的最后你可以问:你想再玩一次吗?如果他们回答否,那么你可以通过检查为 False,否则它将再次循环 -
@Mazen 是的。如果用户选择“n”,我想结束程序,如果“y”,我想从打印(“数学”)打印(“科学”)打印(“英语”)开始循环
标签: python