我一直试图进行随机测验,但不能使字典有效,请帮助!到目前为止这是我的代码。
Import random
questions_answers = {"Which is the comparison operator for Not Equal to? Enter the number of the correct answer, 1. = 2. == 3. != ":"3",
"How many paths through a program does an IF statement allow? 1. One path 2. Two paths 3. Three paths":"2",
"What is this curly bracket called { } ? ": "Braces",
"What does the statement 'print' do? 1. Output a hard copy of a program to a printer 2. Output a message on the screen 3. Print a hard copy of a flowchart to a printer":"2.",
"How many statements are there in this line of code: print('If I am 17, I can drive a car')? 1. There are two statements - 'print' and 'if' 2. There are no statements 3. There is one statement - 'print'":"1",
"What is a variable? 1. A variable is a number 2. A variable is a message input by the user 3. A variable is a location in memory that we use to store data": "3",
"In programming what name is given to the data type which represents decimal numbers?": "Float",
"In programming, what is iteration? 1. The repetition of steps within a program 2. The order in which instructions are carried out 3. A decision point in a program": "1",
"What is the name given to a loop that continues indefinitely? 1. An infinite loop 2. A forever loop 3.A continuous loop": "1",
"What values does a Boolean expression have? 1. Yes or No 2.True or False 3.Right or Wrong": "2",
"How many elements would the array 'student_marks(10)' hold? ": "10",
"In the array 'colours('Purple', 'Blue', 'Red', 'Green', 'Yellow')', what is the value of the element colours(3)?": "Green",
"What is the difference between an array and a list? 1. An array holds numbers, whereas a list holds strings 2. An array can only hold data of the same data type, whereas lists can hold values of different data types 3.An array holds both numbers and strings, whereas a list only holds numbers": "2",
"Why are functions used? 1. To make code more readable 2. To reduce repetition of code and to calculate a value 3. To make decisions": "2",
"What is a bug? 1. An error in a program 2. An error in an algorithm 3. A way of halting a program that is running": "1",
"What is a syntax error? 1. A mistake in the logic of the program 2. A spelling or grammatical mistake in the program 3. A way of halting a program that is running": "2",
"Which of the following contains a syntax error? 1. print('We love computing') 2. print('We love computing') 3. print(answer)": "2",
"Why are comments included in code? 1. So that the program's title is known 2. To make it clear who wrote the program 3. To help programmers understand how the program works": "3",
"If a syntax error is present, what will happen when the error is encountered? 1. The program will crash when the error is encountered 2. The program will behave unexpectedly 3. The program will not start": "1",
"What is an array? 1. A list of variables 2. A list of strings 3. A series of memory locations, each with the same name, that hold related data": "3"}
def ask_questions():
print ("You may quit this game at any time by pressing the letter Q")
while True:
rand_num == questions_asked
if input==answer_list subposition:
print ("Correct!")
score +=1
else:
print ("Incorrect Answer, the correct answer is" + answer_list(rand_num)
break
答案 0 :(得分:0)
好吧,我决定这样做,因为我的python有点生疏,我认为问题很有趣。
import sys
import random
def ask_questions():
score = 0
print ("You may quit this game at any time by pressing the letter Q")
while True:
rand_q = random.choice(questions_answers.keys())
rand_q_answer = int(questions_answers[rand_q])
user_input = input("Your answer? ")
if user_input == rand_q_answer:
print ("Correct!")
score +=1
else:
print ("Incorrect Answer, the correct answer is ", rand_q_answer)
sys.exit()
ask_questions()
此代码似乎运行正常,但我确信它可以改进。