我一直在研究这段代码几个小时,我不确定问题是什么。
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()
错误:
追踪(最近的呼叫最后):
文件&#34; C:\ Users \ user \ Documents \ CharlieStockham \ cgsca \ ca2.py&#34;,第117行,in 运行()
文件&#34; C:\ Users \ user \ Documents \ CharlieStockham \ cgsca \ ca2.py&#34;,第113行,在运行中 保存(name,plr_class,Q())
文件&#34; C:\ Users \ user \ Documents \ CharlieStockham \ cgsca \ ca2.py&#34;,第74行,保存 lines = FileUP(plr_name,score,lines) NameError:全局名称&#39; FileUP&#39;未定义
答案 0 :(得分:2)
第110行:
name, plr_class = details()
但是details
函数没有返回任何内容 - 因此Python尝试将默认返回值None
分配给元组name, plr_class
。它不能这样做,因为None
不是可迭代的(你不能为它分配两个东西)。要解决此问题,请将以下行添加到details
函数中:
return (plr_name, plr_class)
(我没有测试过这个。)
答案 1 :(得分:1)
我喜欢你的游戏,但它有点像mofo:P
score
和qno
未正确定义。在需要它们的函数中定义它们,全局定义它们或将它们作为参数传递给相关的函数。
details()
不会返回任何内容,但您仍尝试使用其输出来定义另外两个变量。将return (plr_name, plr_class)
添加到details()
每次将用户输入转换为int而不检查其值时,如果无法转换int,程序将崩溃。这适用于:
option = int(input("Please select option: "))
这里
plr_ans = int(input())#Gets the user's answer
和其他地方。
由于您的程序输入很多,您可以创建一个函数,您可以将预期的数据类型和可选字符串传递给用户。这样你就不必编写try / except 10次,你的程序不会因意外输入而崩溃。
在def fileUP(plr_name, score, line ):
中,您for line in lines:
但lines
未定义。因此,调用save()
的{{1}}函数也会失败。此外,FileUP()
和FileUP
不是一回事。您使用大写“f”调用函数,但函数的定义将其称为fileUP
,小写为“f”。
虽然我们处于此状态,fileUP
中的文件处理看起来很奇怪。在Python中打开简单读写文件的标准方法是def save (plr_name, plr_class, score):
。
with open()
应该使用一个或两个参数,但目前不会这样,因此会出现此错误:
disp_list()
这两个位置参数在这里给出:
TypeError: disp_list() takes 0 positional arguments but 2 were given