首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python2.7测试

Python2.7测试
EN

Code Review用户
提问于 2017-12-15 20:11:28
回答 2查看 335关注 0票数 8

我做了这个测试,但是代码看起来真的很长。有人能找到使我的代码更短的方法吗?短得多是最好的,但任何事情都有帮助。我认为主要的问题是问题的重复。

代码语言:javascript
复制
choice=raw_input("What type of quiz do you want to do: maths or science? ") 
topic=open("topic.txt", "a+") 
topic.write(choice + '\n') 
topic.close() 
difficulty=raw_input("What difficulty do you want to play on: easy, medium or hard? ") 
diff=open("difficulty.txt", "a+") 
diff.write(difficulty + '\n') 
diff.close()

score = 0


def str_answercheck(score, user_answer, str_answer):
    if user_answer.upper()==str_answer:
        print("Well done")
        score+=1
    else:
        print ("Wrong, it was "+str_answer)
    raw_input("Press enter to continue")
    return score

def int_answercheck(score, user_answer, int_answer):
    if int(user_answer.upper())==int_answer:
        print("Well done")
        score+=1
    else:
        print ("Wrong, it was "+str(int_answer))
    raw_input("Press enter to continue")
    return score

if choice.lower() == "maths" and difficulty.lower() == "easy": 
    easym=open("mathseasy.txt" , "r") 
    lines = easym.readlines() 
    print lines[0]
    print lines[1]
    print("A. 4"+'\n'+"B. 6") 
    str_answer_one="A"
    int_answer_one=4
    user_answer_one=raw_input("Select your answer. ")
    if user_answer_one.isdigit()==True:
        first_score=int_answercheck(score, user_answer_one, int_answer_one)
    else:
        first_score=str_answercheck(score, user_answer_one, str_answer_one)

    print lines[2]
    print("A. 5"+'\n'+"B. 6") 
    str_answer_two="A"
    int_answer_two=5
    user_answer_two=raw_input("Select your answer. ")
    if user_answer_two.isdigit()==True:
        second_score=int_answercheck(first_score, user_answer_two, int_answer_two)
    else:
        second_score=str_answercheck(first_score, user_answer_two, str_answer_two)

    print lines[3]
    print("A. 15"+'\n'+"B. 20") 
    str_answer_three="B"
    int_answer_three=20
    user_answer_three=raw_input("Select your answer. ")
    if user_answer_three.isdigit()==True:
        third_score=int_answercheck(second_score, user_answer_three, int_answer_three)
    else:
        third_score=str_answercheck(second_score, user_answer_three, str_answer_three)

    print lines[4]
    print("A. 13"+'\n'+"B. 15") 
    str_answer_four="A"
    int_answer_four=13
    user_answer_four=raw_input("Select your answer. ")
    if user_answer_four.isdigit()==True:
        fourth_score=int_answercheck(third_score, user_answer_four, int_answer_four)
    else:
        fourth_score=str_answercheck(third_score, user_answer_four, str_answer_four)

    print lines[5]
    print("A. 100"+'\n'+"B. 110") 
    str_answer_five="B"
    int_answer_five=110
    user_answer_five=raw_input("Select your answer. ")
    if user_answer_five.isdigit()==True:
        fifth_score=int_answercheck(fourth_score, user_answer_five, int_answer_five)
    else:
        fifth_score=str_answercheck(fourth_score, user_answer_five, str_answer_five)




if choice.lower() == "maths" and difficulty.lower() == "medium": 
    mediumm=open("mathsmedium.txt" , "r") 
    lines = mediumm.readlines() 
    print lines[0]
    print lines[1]
    print("A. 30"+'\n'+"B. 35") 
    str_answer_one="A"
    int_answer_one=30
    user_answer_one=raw_input("Select your answer. ")
    if user_answer_one.isdigit()==True:
        first_score=int_answercheck(score, user_answer_one, int_answer_one)
    else:
        first_score=str_answercheck(score, user_answer_one, str_answer_one)

    print lines[2]
    print("A. 100"+'\n'+"B. 110") 
    str_answer_two="B"
    int_answer_two=110
    user_answer_two=raw_input("Select your answer. ")
    if user_answer_two.isdigit()==True:
        second_score=int_answercheck(first_score, user_answer_two, int_answer_two)
    else:
        second_score=str_answercheck(first_score, user_answer_two, str_answer_two)

    print lines[3]
    print("A. 13"+'\n'+"B. 15") 
    str_answer_three="A"
    int_answer_three=13
    user_answer_three=raw_input("Select your answer. ")
    if user_answer_three.isdigit()==True:
        third_score=int_answercheck(second_score, user_answer_three, int_answer_three)
    else:
        third_score=str_answercheck(second_score, user_answer_three, str_answer_three)

    print lines[4]
    print("A. 30"+'\n'+"B. 32") 
    str_answer_four="B"
    int_answer_four=32
    user_answer_four=raw_input("Select your answer. ")
    if user_answer_four.isdigit()==True:
        fourth_score=int_answercheck(third_score, user_answer_four, int_answer_four)
    else:
        fourth_score=str_answercheck(third_score, user_answer_four, str_answer_four)

    print lines[5]
    print("A. 21"+'\n'+"B. 29") 
    str_answer_five="B"
    int_answer_five=29
    user_answer_five=raw_input("Select your answer. ")
    if user_answer_five.isdigit()==True:
        fifth_score=int_answercheck(fourth_score, user_answer_five, int_answer_five)
    else:
        fifth_score=str_answercheck(fourth_score, user_answer_five, str_answer_five)

以下是mathseasy.txt中的数据:

代码语言:javascript
复制
Welcome to the easy maths quiz.
What's 2+2?
What's 11-6?
What's 5*4?
What's 26/2?
What's 11*10?

以下是mathsmedium.txt中的数据:

代码语言:javascript
复制
Welcome to the medium maths quiz.
What's 5*6?
What's 79+31?
What's 26/2?
What's 4*8?
What's 50-21
EN

回答 2

Code Review用户

发布于 2017-12-15 22:03:43

下面是一些缩短代码的方法:

  1. 您的str_int_answercheck基本上是相同的函数,除了一个布尔表达式。与传递和比较这些值不同,为什么不编写一个使用布尔值并执行以下工作的底层函数: def check_answer(was_correct):如果was_correct: print("Well to“) score+=1 score+=1(”错误,它是"+str_answer) raw_input(“按下回车继续”)返回分数str_answercheck(user_answer,str_answer):返回check_answer(user_answer.upper() == str_answer.upper()) def int_answercheck(user_answer,int_answer):返回check_answer(int(user_answer) == int_answer)
  2. 您编写了两个应答检查函数,但始终使用if语句来确定要调用哪个函数。将该逻辑放入一个单一的应答检查函数中: def应答检查(user_answer,str_answer,int_answer):if user_answer.isdigit():返回int_answercheck(user_answer,int_answer),否则:返回str_answercheck(user_answer,str_answer)
  3. 如果答案检查返回true,则执行加法,而不是更新全局得分。这并不能缩短您的代码,但它是良好的编码风格(在不需要时避免使用全局变量):如果回答检查(user_answer,str_answer,int_answer):得分+= 1
  4. 您正在从文件中打印行,然后从程序源代码中打印允许的答案。这意味着测试文件和源代码是同步的-它们不是相互独立的。既然源代码是常量的,为什么不把可能的答案放在测试文件中:欢迎参加简单的数学测试。2+2是什么?什么是11-6?如果您做了这些更改,您可以转换以下代码:0打印行1 print (“A.30”+‘\n’+“B.35 ") str_answer_one="A”int_answer_one=30 user_answer_one=raw_input(“选择您的答案”)。如果user_answer_one.isdigit()==True: first_score=int_answercheck(得分,user_answer_one,int_answer_one),其他:first_score=str_answercheck(得分,user_answer_one,( str_answer_one)变成这样的代码:标题=行0打印头打印行1#问句打印行2#答案1打印行3.#答案2 user_answer =raw_input(“选择您的答案:") str_answer = "A”int_answer = 30如果回答检查(user_answer,str_answer,int_answer):分数+= 1
  5. 如果你有一种从输入文件中读取正确答案的方法就好了!哦,等等!欢迎来到简单的数学测验。2+2是什么?答: A. 4.什么是11-6?答:通过在文件中提供答案,你可以使你的代码更通用。(注:有一个明显的循环解决方案。但是,我在代码中没有看到任何循环,所以我假设您在学习中还没有达到这个阶段。)def问题(名词,行):I= num *4+1打印行我#问句打印行I+1#答案1打印行I+2#答案2 user_answer =raw_input(“选择答案:")答案=行I+3.split()#”答案:“,"5”str_answer,int_answer =应答1,int(答案2)返回答案检查(user_answer,str_answer,int_answer):如果答案正确,这将允许您问每个问题并返回真假: score =0如果问题(0,行):得分+= 1如果问题(1,行):得分+= 1如果问题(2,行):分数+= 1(当您到达那里时,循环版本应该是显而易见的)。
  6. 您正在处理不同程度的困难,作为单独的代码块。这是必要的,因为您的代码内置了答案。但有了问卷文件中的答案,你就不用再这么做了。相反,只需打开文件并在一个地方处理这些问题:问句= subject.lower() + difficulty.lower() + ".txt“,打开(问题)为幼稚:line= infile.readlines()打印行0评分=0如果问题(0,行):score += 1如果问题(1,行):得分+= 1如果问题(2,行):分数+= 1
票数 7
EN

Code Review用户

发布于 2017-12-15 21:16:29

做答案检查的一个较短的方法是将两者结合在一起:

代码语言:javascript
复制
def answercheck(score, user_answer, str_answer, int_answer):
    if user_answer.upper() == str_answer or user_answer.upper() == int_answer:
        print("Well done")
        score+=1
    else:
        print ("Wrong, it was "+str(answer))
    raw_input("Press enter to continue")
    return score

注意:这意味着代码的其余部分必须进行编辑,才能始终发送到answercheck

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/182891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档