问答游戏

import sys

def open_file(file_name,mode):
    """Open a file."""
    try:
        the_file = open(file_name,mode)
    except IOError as e:
        print("Unable to open the file",file_name,"Ending program.\n",e)
        input("\n\nPress the enter key to exit.")
        sys.exit()
    else:
        return the_file

def next_line(the_file):
    """Return next line from the trivia file,formatted"""
    line = the_file.readline()
    line = line.replace("/","\n")
    return line

def next_block(the_file):
    """Return the next block of data from the trivia file."""
    category = next_line(the_file)
    
    question = next_line(the_file)
    
    answer = []
    for i in range(4):
        answer.append(next_line(the_file))
    
    correct = next_line(the_file)
    if correct:
        correct = correct[0]
    
    explanation = next_line(the_file)
    
    return category,question,answer,correct,explanation

def welcome(title):
    """Welcome the player and get his/her name."""
    print("\t\tWelcome to trivia Challenge!\n")
    print("\t\t",title,"\n")
    
def main():
    trivia_file = open_file("trivia.txt","r")
    title = next_line(trivia_file)
    welcome(title)
    score = 0
    
    category,question,answers,correct,explanation = next_block(trivia_file)
    while category:
        print(category)
        print(question)
        for i in range(4):
            print("\t",i+1,"-",answers[i])
        answer = input("What's your answer?:")
        if answer == correct:
            print("\nRight!",end=" ")
            score += 1
        else:
            print("\nWrong.",end=" ")
        print(explanation)
        print("Score:",score,"\n\n")
        category,question,answers,correct,explanation = next_block(trivia_file)
    
    trivia_file.close()
    
    print("That was the last question")
    print("You're final score is ",score)

main()
		Welcome to trivia Challenge!

		 An Episode You Can't Refuse
 

On the Run With a Mammal

Let's say you turn state's evidence and need to"get on the lamb."If you wait 
too long,what will happen?

	 1 - You'll end up on the sheep

	 2 - You'll end up on the cow

	 3 - You'll end up on the goat

	 4 - You'll end up on the emu

What's your answer?:1

Right! A lamb is just a young sheep

Score: 1 


The Godfather will Get Down With You Now

Let's say you have an audience with the Godfather of Soul.How would it be
smart to adress him?

	 1 - Mr.Richard

	 2 - Mr.Domino

	 3 - Mr.Brown

	 4 - Mr.Checker

What's your answer?:3

Right! James Brown is the Godfather of Soul.
Score: 2 


That was the last question
You're final score is  2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值