2015-10-30 35 views
-2
import time 
from random import randint 
import sys 
import random 
Level = 0 
ONE = 10 
TWO = 20 
THREE = 60 
FOUR = 100 
FIVE = 150 
QN = 0 
TEN = 10 
Zero = 0 
Chances = 5 
Score = 0 
Name = input("What is your name? \n").title() 
time.sleep(1) 
class_name = input("What class are you in? \n").upper() 
time.sleep(1) 
print("Welcome!\nTo the Ten Question Quiz {}!!".format(Name)) 
time.sleep(1) 
operation = ['+','-','*'] #holds all of the opperators 
RO = random.choice(operation)#chooses a random operator 
def Quiz(): 
    RoG = input(Name+" Please type 'rules' to view the rules or Press 'ANY' key to Continue\n*********************************************************************************************************\n") 
    if RoG == "rules": 
     time.sleep(1) 
     print("*********************************************************************************************************") 
     print("*RULES/HOW TO PLAY* \nThere will be 10 Questions for you to answer \nthere WILL be a timer, total time taken to finish the quiz \nYou will be able to choose the Level from 1, 2, 3\n1 = Very Easy, 2 = Easy, 3 = Medium, 4 = Hard, 5 = Very Hard \nYou will only have '5' chances to answer, 'ALL' The 10 Questions.") 
     input("Press 'ANY' key to Return to the previous page\n*********************************************************************************************************") 
     Quiz() 
    else: 
     LV = input("Please Choose Your Level From (1, 2, 3, 4, 5)\n") 
     if LV == "1": 
      Level = ONE 
     elif LV == "2": 
      Level = TWO 
     elif LV == "3": 
      Level = THREE 
     elif LV == "4": 
      Level = FOUR 
     elif LV == "5": 
      Level = FIVE 
     def Timer(): 
      start_time = time.time() 
      def Q1(): 
       global Chances 
       while Chances > Zero: 
        global QN 
        QN = QN+1 
        if str(QN) == "10": 
         time.sleep(1) 
         print("You have Finished The Ten Question Quiz {} !!".format(Name)) 
         time.sleep(1) 
         end_time = time.time() 
         print("Total time taken was %g seconds." % (end_time - start_time)) 
         print("Your Score is {} /10".format(Score)) 
         Class_name = class_name + ".txt" #adds '.txt' to the end of the file so it can be used to create a file under the name a user specifies 
         file = open(Class_name , 'a') #opens the file in 'append' mode so you don't delete all the information 
         name = (Name) 
         file.write(str(name + " " + class_name + ", Your score is: ")) #writes the information to the file 
         file.write(str(Score)) 
         file.write("/10 Level: ") 
         file.write(str(LV)) 
         file.write(", Total time taken was %g seconds." % (end_time - start_time)) 
         file.write('\n') 
         file.close() #safely closes the file to save the information 
         viewscore = input("Do you wish to view previous results for your class:(yes/no) \n").lower() 
         if viewscore == "yes".lower(): 
          f = open(Class_name, 'r') 
          file_contents = f.read() 
          print (file_contents) 
          f.close() 
          if input("Press 'Y' to exit \nor press any key to start again.").lower() == "y": 
           sys.exit() 
          else: 
           ex = input("") 
           Quiz() 
         elif viewscore != "yes".lower(): 
          if input("Press 'Y' to exit \nor press any key to start again.").lower() == "y": 
           sys.exit() 
          else: 
           ex = input("") 
           Quiz() 
        else: 
         print("Starting Question {}".format(QN)) 
         Ran1 = randint(1,Level) 
         Ran11 = randint(1,Level) 
         time.sleep(1) 
         RO = random.choice(operation) #chooses a random operator 
         Answer1=int(eval(str(Ran1) + RO + str(Ran11))) 
         print ("What is {} {} {}?".format(Ran1, RO, Ran11)) 
         if input() == str(Answer1): 
          print("Thats the Correct Answer WELL DONE!") 
          Score = Score+1 
          print("*********************************************************************************************************") 
          Q1() 
         else: 
          Chances = Chances-1 
          print("Wrong!\nThats the wrong answer, you got {} Chance(s)".format(Chances)) 
          print("*********************************************************************************************************") 
          Q1() 
      Q1() 
     Timer() 
Quiz() 

我知道这里设置的一些变量是不需要的,但我会在将来修复它们;所有我想知道的是如何解决此错误消息:UnboundLocalError:分配前引用的局部变量'Score'

Traceback (most recent call last): 
    File "N:/IMPORTANT/Assessment2222 - Copyc.py", line 105, in <module> 
    Quiz() 
    File "N:/IMPORTANT/Assessment2222 - Copyc.py", line 104, in Quiz 
    Timer() 
    File "N:/IMPORTANT/Assessment2222 - Copyc.py", line 103, in Timer 
    Q1() 
    File "N:/IMPORTANT/Assessment2222 - Copyc.py", line 95, in Q1 
    Score = Score+1 
UnboundLocalError: local variable 'Score' referenced before assignment 
>>> 

我已经试过global Score,但随后说;

>>> 
Warning (from warnings module): 
    File "N:/IMPORTANT/Assessment2222 - Copyc.py", line 95 
    global Score 
SyntaxWarning: name 'Score' is used prior to global declaration 

所以现在我没有选择。

+0

*你在哪里试过'全球分数'?虽然这不是最好的方法,但如果你在“测验”开始时提出这个问题,我希望它能够奏效。 – jonrsharpe

+0

我把它放在Quiz()函数之上,它没有工作,我在Score = Score + 1之上做了它。 –

+0

请参阅http://stackoverflow.com/q/423379/3001761。或者,再次,*停止使用全局变量*。 – jonrsharpe

回答

0

函数内部的变量的范围是local。你可以这样做(声明变量函数内部)

def Quiz(): 
    Score = 0 

或使用global(不推荐)

Score = 0 
def Quiz(): 
    global Score 
    Score = Score + 1 

UPDATE

由于在函数内部定义的函数,

def Q1(): 
    global Score 
+0

我这样做了它相同的概率 –

+0

把'全球分数'放在测验功能的第一行 – itzMEonTV

+0

分数之前表示分数,那就是它说的 –

0

i trie d做你做了什么,但它没有工作,所以我采取了同样的想法,并把高清Q1(后的总体得分),而不是和它的工作..

def Q1(): 
    global Score 

和THX很多UR 4帮助:)

相关问题