2016-03-08 68 views
0

我一直在研究这段代码几个小时,现在我不确定问题出在哪里。难以执行的程序

import random#imports random 
import os#Imports os 
print("Welcome to the maths quiz") # Welcomes user to quiz 
score = (0) 


def details(): 
    plr_name = input ("Please Input Name:") # Asks user for name 
    plr_class = input("Input class number: ") # Asks the user for class numer 
return (plr_name, plr_class) 

def Q(): 
    while qno < 10: # loops while qno is under 10 
     ran_num1 = random.randint(1,99) # Generates the first random number 
     ran_num2 = random.randint(1,99) # Generates the second random number 
     ran_fun = random.choice("X-+") # Picks a random function 
     print(ran_num1,ran_fun,ran_num2,"=") # Prints the Sum for the user 
     if ran_fun == "X": 
      sum_ans = ran_num1 * ran_num2 # Does the sum if it is a multiplication 
     if ran_fun == "+": 
      sum_ans = ran_num1 + ran_num2 # Does the sum if it is a addition 
     if ran_fun == "-": 
      sum_ans = ran_num1 - ran_num2 # Does the sum if it is a subtraction 
     plr_ans = int(input()) # Gets the user's answer 
     if plr_ans == sum_ans: 
      print("Correct!") # Prints correct 
      score = score + 1 # Adds 1 to score 
     else: 
      print("Incorrect!") 
     qno = qno + 1 # Adds 1 to qno 


def plr_list_make(lines, listoreder): 
    index = 0 
    plr_names =[] 
    plr_scores =[] 

    for line in lines: 
     if listorder == 1: 
      column =0 
      rev = False 
    else: 
     column = 1 
     rev = True 
    return sorted(zip(plr_names, plr_scores),key = lambda x:(x[column]),reverse = rev) 


def fileUP(plr_name, score, line): 
    found = False 
    index = 0 
    for line in lines: 
     if line.startswith(plr_name): 
      line = line.strip("\n") + ","+str(score+"\n") 
      lines[index] = line 
      found = True 
     index = index + 1 
    if not found: 
     lines.append(plr_name+"|" +str(score)+"\n") 
    return lines 


def save (plr_name, plr_class, score): 
    filename = "QuizScore_"+plr_class+".txt" 
    try: 
     fileI = open(filename) 
    except IOError: 
     fileI = open(filename, "w+") 
     fileI = open(filename) 
    lines = fileI.readlines() 
    fileI.close 

    lines = FileUP(plr_name, score, lines) 
    fileO = open(filename, "w") 
    fileO.writelines(lines) 
    fileO.close 


def disp_list(): ## intialise_list 
    student_list=[] 
    filename = "QuizScore_"+plr_class+".txt" 
    try: 
     ## open file read into list "lines" 
     input_file = open(filename) 
     lines = input_file.readlines() ## read file into list "lines" 
     input_file.close 

     student_list = create_student_list(lines, listorder) ### update "lines" with student list as requested by user 

     ## output sorted list 
     for counter in range(len(student_list)): 
      print ("Name and Score: ", student_list[counter][0], student_list[counter][1])  

    except IOError: 
     print ("no class file!!!") 



def menu(): 
    print ("1 Test") 
    print ("2 Alphabetical") 
    print ("3 Highscore") 
    print ("4 Avg Score") 

def Run(): 
    selection = 0 
    while selection != 5: 
     menu() 
     option = int(input("Please select option: "))  
     if option == 1: 
      name, plr_class = details() 
      save(name, plr_class, Q()) 
     else: 
      plr_class = input("input class ") 
      disp_list(plr_class, option-1) 
Run() 

错误:

Traceback (most recent call last):
File "C:\Users\user\Documents\CharlieStockham\cgsca\ca2.py", line 117, in Run()
File "C:\Users\user\Documents\CharlieStockham\cgsca\ca2.py", line 113, in Run save(name, plr_class, Q())
File "C:\Users\user\Documents\CharlieStockham\cgsca\ca2.py", line 74, in save lines = FileUP(plr_name, score, lines) NameError: global name 'FileUP' is not defined

+1

问题标题是可爱,但没有说明问题。 – KWeiss

+0

当我运行该程序时,菜单功能可以正常工作,但是当您输入学生姓名和学生类时会崩溃。 –

+0

你能给我们提供这些错误吗? –

回答

2

行110:

 name, plr_class = details() 

details功能不返回任何东西 - 所以Python会尝试将默认返回值None分配给元组name, plr_class。它不能这样做,因为None不是可迭代的(你不能指定两件事)。为了解决这个问题,下面的行添加到您的details功能:

return (plr_name, plr_class) 

(我没有测试过这一点。)

+1

代码中的99个错误修复了一个错误再次运行它代码中的137个错误:) – walkingRed

1

我喜欢你的游戏,但它的马车作为美富:P

scoreqno未正确定义。在需要它们的函数中定义它们,全局定义它们或将它们作为参数传递给相关函数。

details()不会返回任何东西,但仍尝试使用其输出来定义两个其他变量。将return (plr_name, plr_class)添加到details()

每次将用户输入强制转换为int而未检查其值时,如果无法转换int,则程序将崩溃。这适用于这里:

option = int(input("Please select option: ")) 

这里

plr_ans = int(input())#Gets the user's answer 

和其他地方。

因为你的程序是输入重,你可以做一个功能,您通过预期的数据类型和一个可选的字符串来显示给用户。这样你就不必写试/除了10次,你的程序不会在意外输入时崩溃。

def fileUP(plr_name, score, line):你有for line in lines:lines没有定义。因此,调用FileUP()save()函数也会失败。另外,FileUPfileUP不是一回事。你用大写字母“f”调用该函数,但该函数的定义将其称为fileUP,小写字母“f”。

虽然我们在这,在def save (plr_name, plr_class, score):文件处理看起来怪怪的。在Python中打开用于简单读取和写入的文件的标准方式是通过with open()

disp_list()应该采取一个或两个参数,但在此刻所以引发此错误不会:这里给出

TypeError: disp_list() takes 0 positional arguments but 2 were given 

这2个位置论点:

disp_list(plr_class, option-1)