用Python构建随机多选测验.如何提出其他问题



我是一名新程序员,正在编写我的第一个Python程序。

我正在用Python构建一个小问答游戏。该代码包含十个可能的测验问题。为了获胜,用户必须连续获得四个正确的答案。为了让游戏更具可玩性,我设置了分配给随机数生成器的问题。

到目前为止,一切都很好,但我不知道如何让程序去BACk,并在用户完成第一个问题后再次运行数字生成器。我想我不太明白如何构建循环?

这是我的密码。我想有一种更优雅的方式来写这篇文章,但由于每个问题都有不同的多选选项,我不确定把所有问题都放在一个优雅的循环中的最佳方式。

我如何调整它,让用户真正得到不止一个问题?

import random
#Intro message
print("Welcome to Where In The World Is The Civil ConFLiCT Trophy?")
print("Brought to you by Extra Points.")
user_name = input ("What is your name?")
print("Okay, " + str(user_name) + ". Here is the deal.")
print("")
print("Bob Diaco has stolen the critically important college football landmark, the Civil ConFLiCT Trophy.")
print("That trophy belongs in a musuem! Or at least, somewhere in the catacombs of the UCF athletic department.")
user_choice = input ("Will you help us find it?")
#decision tree around "will you help us find it"
if user_choice == "no":
print("then why the hell did you play this game?")
elif user_choice == "yes":
print("Great. Let's follow our first clue.")
else:
print("Please answer yes or no. I'm too stupid to program anything else.")

#here is where I'm gonna try to put my question variables
question_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
user_score = 0
wrong_answer = "Sorry, that was incorrect. Now Bob Diaco got away."
right_answer = "Correct! To the next clue!"
#The goal here is for the user to answer four questions correctly.
#Each correct answer should increase their score by one. A failed answer should end the game.

computer_action = random.choice(question_list)
if computer_action == 1:
print("Diaco is on the run towards the school with the smallest enrollment in FBS. Where should we go?")
print("A. Navy.")
print("B. Tulsa.")
print("C. SMU.")
print("D. Rice.")
user_answer_1 = input("Please answer A, B, C or D.")
if user_answer_1 == "B":
print(right_answer)
user_score + 1
computer_action = random.choice(question_list)
else:
print(wrong_answer)

elif computer_action == 2:
print("That dastardly Diaco absconded with the trophy and is off to the city that hosted the Salad Bowl, which was a real college football game that we didn’t make up.")
print("A. Phoenix, AZ")
print("B. Cleveland, OH")
print("C. Evansville, IN")
print("D. Santa Fe, NM.")
user_answer_2 = input("Please answer A, B, C or D.")
if user_answer_2 == "A":
print(right_answer)
user_score + 1
computer_action = random.choice(question_list)
else:
print(wrong_answer)
elif computer_action == 3:
print("Now Diaco has fled to the campus of the last D-1 school to drop football completely. Note: I said D-1, NOT FBS! ")
print("A. Hofstra.")
print("B. Idaho.")
print("C. Jacksonville.")
print("D. Seton Hall.")
user_answer_3 = input("Please answer A, B, C or D.")
if user_answer_3 == "C":
print(right_answer)
user_score + 1
computer_action = random.choice(question_list)
else:
print(wrong_answer)

elif computer_action == 4:
print("Bob Diaco has taken the trophy and fled to the only school on this list where John Heisman did NOT coach.")
print("A. Wooster")
print("B. Oberlin")
print("C. Auburn")
print("D. Rice")
user_answer_4 = input("Please answer A, B, C or D.")
if user_answer_4 == "A":
print(right_answer)
user_score + 1
computer_action = random.choice(question_list)
else:
print(wrong_answer)

elif computer_action == 5:
print("So close! But we just missed it. Now Diaco has taken the trophy to where Amos Alonzo Stagg coached immediately after leaving Chicago. ")
print("A. Northwestern")
print("B. Notre Dame")
print("C. San DIego State")
print("D. Pacific")
user_answer_5 = input("Please answer A, B, C or D.")
if user_answer_5 == "D":
print(right_answer)
user_score +1
computer_action = random.choice(question_list)
else:
print(wrong_answer)
elif computer_action == 6:
print("Gasp, we missed it again! Now, the trophy is hiding at the stadium with the smallest capacity in FBS. Or at least, according to Wikipedia.")
print("A. Coasta Carolina.")
print("B. Charlotte.")
print("C. Ball State.")
print("D. Nevada.")
user_answer_6 = input("Please answer A, B, C or D.")
if user_answer_6 == "B":
print(right_answer)
user_score + 1
computer_action = random.choice(question_list)
else:
print(wrong_answer)

elif computer_action == 7:
print("Aww hamburgers, the trophy is on the move again! This time, it’s allegedly bound for the campus of the P5 program with the worst bowl winning percentage!")
print("A. Wake Forest.")
print("B. Duke.")
print("C. Indiana.")
print("D. Iowa State.")
user_answer_7 = input("Please answer A, B, C or D.")
if user_answer_7 == "C":
print(right_answer)
user_score + 1
computer_action = random.choice(question_list)
else:
print(wrong_answer)
elif computer_action == 8:
print("Rats, it looks like we just missed it! Now I hear the trophy is headed to the campus with the highest winning percentage in all of FBS football.")
print("A. Ohio State.")
print("B. Michigan.")
print("C. Notre Dame.")
print("D. Boise State.")
user_answer_8 = input("Please answer A, B, C or D.")
if user_answer_8 == "D":
print(right_answer)
user_score + 1
computer_action = random.choice(question_list)
else:
print(wrong_answer)
elif computer_action == 9:
print("Legend has it, the trophy was mailed to the campus of the FBS record holder for most passing touchdowns in a single game.")
print("A. Hawaii.")
print("B. Houston.")
print("C. Texas Tech.")
print("D. Wisconsin.")
user_answer_9 = input("Please answer A, B, C or D.")
if user_answer_9 == "B":
print(right_answer)
user_score + 1
computer_action = random.choice(question_list)
else:
print(wrong_answer)
elif computer_action == 10:
print("Rumor has it, the trophy is off to the city where Woody Hayes got his first head coaching job!.")
print("A. Columbus, OH.")
print("B. Oxford, OH.")
print("C. Granville, OH.")
print("D. Athens, OH.")
user_answer_10 = input("Please answer A, B, C or D.")
if user_answer_10 == "C":
print(right_answer)
user_score + 1
computer_action = random.choice(question_list)
else:
print(wrong_answer)

if user_score == 4:
print("Congrats! You've finally aprehended Bob Diaco and the mythical Civil ConFliCT Trophy. It can now be sent to the College Football Hall of Fame to be properly enjoyed by everybody. Except UConn, probably.")
print("")








Put:

while user_score < 4:

在正上方的线路上

computer_action = random.choice(question_list)

然后取while下面的每一行,并将其制表一次,将所有这些都放入while循环中。(如果你使用的是一个不错的文本编辑器,你应该可以选择所有这些行并点击tab。如果不是,我强烈建议你切换到Notepad++或Thonny之类的(

然后删除检查用户分数的if语句(这是while循环的工作。(;你赢了"循环下面和外面的东西。

每当程序到达while循环的底部时,如果循环头中的条件(在本例中为user_score < 4(仍然为真,它就会跳到顶部,否则它就会离开循环。因此在这种情况下,代码将退出循环并打印";恭喜"当用户的分数不再小于4时。

相关内容

最新更新