2016-09-21 6 views
-4

나는 무작위로 퀴즈를 만들려고했지만 사전을 작동시킬 수 없다. 제발 도와주세요! 지금까지 내 코드입니다. 내 파이썬은 약간 녹슨 내가 질문이 재미 있었다 생각했기 때문에사전을 사용하여 무작위 퀴즈를 만드는 방법은 무엇입니까?

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 
+2

당신의 코드는 95 % 에러입니다,'rand_num == questions_asked', 어디서나 지정되지 않은'rand_num'과 비교하면,'rand_num = a_random_key','input == answer_list subposition'이 테스트 될 것이라고 상상해 봅니다. 만약'input' 함수에 대한 참조가 일부 변수와 동일하다면 작동하지만, 공백 문자와 구문 오류, 그리고 정의되지 않은 변수'subposition'가 없으면 작동하지 않습니다. 여러 들여 쓰기 문제가 있습니다. 튜토리얼 http://anandology.com/python-practice-book/iterators.html에서 시작하는 것이 좋습니다. –

+1

"사전 작업을 할 수 없습니까?"라는 의미가 정확 할 수 있습니까? 오류를 가리키고 있습니다 –

+0

여러가지 문제가 있습니다,'input == answer_list subposition'로 시작하십시오 –

답변

0

좋아, 나는이 작업을 수행하기로 결정했다.

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() 

이 코드는 개선 될 수 있지만 잘 작동하는 것 같습니다.

관련 문제