☆☆ 新着記事 ☆☆

2018年10月4日木曜日

Python Quiz sample code == 写経と読解


参考.1)
https://codereview.stackexchange.com/questions/22822/simple-quiz-program?answertab=votes#tab-top

例)

 #!/usr/bin/env python3
import string
print("Content-type: text/html\n")
print("<html><body>Python is Fabulous !")
NUMBER_OF_ATTEMPTS = 2
ENTER_ANSWER = 'Hit %s for your answer\n'
TRY_AGAIN = 'Incorrect!!! Try again.'
NO_MORE_ATTEMPTS = 'Incorrect!!! You ran out of your attempts'

def question(message, options, correct, attempts=NUMBER_OF_ATTEMPTS):
    '''
    message - string
    options - list
    correct - int (Index of list which holds the correct answer)
    attempts - int
    '''
    optionLetters = string.ascii_lowercase[:len(options)]
    print(message + "<br/><br/>")
    print(' '.join('%s: %s' % (letter, answer) for letter, answer in zip(optionLetters, options))+ "<br/>")
    while attempts > 0:
        response = input(ENTER_ANSWER % ', '.join(optionLetters)) # For python 3
        #response = raw_input(ENTER_ANSWER % ', '.join(optionLetters)) # For python 2
        if response == optionLetters[correct]:
            return True
        else:
            attempts -= 1
            print("TRY_AGAIN")
    print("NO_MORE_ATTEMPTS")
    return False
         
print("Mathematics Quiz"+"<br/>")
         
# question1 and question2 will be 'True' or 'False'
question1 = question('Who is president of USA?', ['myself', 'His Dad', 'His Mom', 'Barack Obama'], 3)
question2 = question('Who invented Facebook?', ['Me', 'His Dad', 'Mark Zuckerberg', 'Aliens', 'Someone else'], 2)
print("</body></html>")


参考.2)

https://codereview.stackexchange.com/questions/153495/simple-multiple-choice-quiz

0 件のコメント:

コメントを投稿