【发布时间】:2016-03-08 10:44:56
【问题描述】:
我已经在这段代码上工作了几个小时,但我不太确定问题出在哪里。
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()
错误:
回溯(最近一次通话最后一次):
文件“C:\Users\user\Documents\CharlieStockham\cgsca\ca2.py”,第 117 行,在 运行()
运行中的文件“C:\Users\user\Documents\CharlieStockham\cgsca\ca2.py”,第 113 行 保存(名称,plr_class,Q())
文件“C:\Users\user\Documents\CharlieStockham\cgsca\ca2.py”,第 74 行,保存 行= FileUP(plr_name,分数,行) NameError: 未定义全局名称“FileUP”
【问题讨论】:
-
问题标题很可爱,但没有说明问题。
-
当我运行程序时,菜单功能可以工作,但是当你输入学生姓名和学生班级时它会崩溃。
-
你能把这些错误告诉我们吗?
-
您的函数 Q() 不返回任何内容,但您将其返回值 (
None) 传递给 save()。也许这是您问题的根源。 -
以下是正在发生的错误: Traceback(最近一次调用最后一次):文件“C:\Users\user\Documents\IBliminse\cgsca\ca2.py”,第 115 行,在
Run() 文件“C:\Users\user\Documents\IBliminse\cgsca\ca2.py”,第 110 行,在运行名称中,plr_class= details() TypeError: 'NoneType' object is not iterable
标签: python