"Python" example-answer system

Source: Internet
Author: User

#!/usr/bin/env pythonfrom operator Import Add, Subfrom random import randint, Choiceops = {' + ': Add, '-': sub}maxtries = 2 Def doprob (): op = Choice (' +-') Nums = [Randint (1,10) for I in range (2)] Nums.sort (reverse=true) ans = ops[o P] (*nums) PR = '%d%s%s = '% (Nums[0], op, nums[1]) oops = 0 while True:try:if int (raw_inpu T (pr)) = = ans:# #只有正确作答才跳出循环 print ' correct ' break if oops = = maxtries:# #第二次给出正确答案 , but still the title print ' Sorry ... the answer is\n%s%d '% (pr, ans) else:print ' Incorrec            T ... try again ' oops + = 1 except (Keyboardinterrupt, Eoferror, ValueError): print ' Invalid input ... try again ' def main (): While True:doprob () try:opt = Raw_input (' Aga In?            [y] '). Lower () if opt and opt[0] = = ' n ': Break except (Keyboardinterrupt, Eoferror): Breakif __name__ = = ' __main__ ': Main () 

"Python" example-answer system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.