I would like to total the amount of correct answers and display it at the end getting error local variable referenced
只是想知道为什么错误会说没有定义名称错误名称问题1
计数=0随机进口
我的问题列表=[问题1,问题2,问题3,问题4,问题5,问题6,问题7,问题8,问题9,问题10,问题11,问题12,问题13,问题14,问题15,问题16,问题17,问题18,问题19,问题20,问题21,问题22,问题23,问题24,问题25,问题26,问题27,问题28,问题29,问题30,问题31,问题32,问题33,问题34,问题35,问题36,问题37,问题38,问题39,问题40]
定义简介(开始):如果开始=是或开始=是:print("开始吧。")其他:打印("感谢您查看!再见!
定义问题1():全局计数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | text_file = open("q1.txt") print (text_file.read()) text_file.close item ="" ans1 ="B" while item !="A" and item !="B" and item !="C" and item !="D": item = input() if item == (ans1): print ("Correct") count += 1 else: print("Incorrect") return count |
DEF主体():打印("你好!欢迎来到全新的琐碎追求!")print("你有20个选择题和20个对错。")打印("每个问题都是一点。您的分数将在末尾显示。")打印("要开始吗?按Y或是。请用大写字母回答所有问题。")
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | start = input() intro(start) random.choice(my_list)() question1() question2() question3() question4() question5() question6() question7() question8() question9() question10() question11() question12() question13() question14() question15() question16() question17() question18() question19() question20() question21() question22() question23() question24() question25() question26() question27() question28() question29() question30() question31() question32() question33() question34() question35() question36() question37() question38() question39() question40() print("You got", count,"right out of 40!") |
主体()
您可以使
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | count = 0 def intro(start): if start =="yes" or start =="y": print("Lets begin.") else: print("Thanks for checking it out! Bye Bye!") def question1(): global count count += 1 return count def question2(): global count count += 1 return count def main(): print("Hello There! Welcome to an all new Trivial Pursuit!") print("You have twenty multiple choice questions and twenty true and false.") print ("Each question is one point. Your score will be presented at the end.") print("Would you like to begin? Press y or yes") start = input() intro(start) question1() question2() print("You got", count,"right!") main() |
我编辑了您的代码,让这些函数添加到