2016-01-28 25 views
0
import random 

correct= 0 
print ("Hello, Welcome to the Quiz!") 
name = input("What is your name?") 
class_no = "" 
while class_no not in ["1", "2", "3"]: 
    class_no = input("Please enter your class - 1,2 or 3:") 
print ("Welcome to this maths quiz, while answering the questions please take in mind:") 
print ("That + is addition, - subtraction and * is multiplication") 
print ("Also please only enter numbers and make sure you do not leave an answer blank, Thank you!") 
for count in range(10): 
    num1 = random.randint(1,20) 
    num2 = random.randint(1,10) 
    symbol = random.choice(["+","-","*"]) 
    print("Please solve :\n",num1,symbol,num2) 
    user = int(input("")) 

    if symbol == "+": 
     answer = num1 + num2 
    elif symbol == "-": 
     answer = num1 - num2 
    elif symbol == "*": 
     answer = num1 * num2 


    if user == answer: 
     print("Correct!") 
     correct = correct + 1 
    else: 
     print("Incorrect") 

print(name ,"You Got ",correct, "Out of 10") 

with open("class%s.txt" % class_no, "a") as my_class: 
    my_class.write("{0}\n".format([name,correct])) 

viewscores= input("Please select a class from 1,2 or 3 and press space and choose one from alphabetically, average or highest?") 
if viewscores=='1 alphabetically':  
    with open('class1.txt', 'r') as r: 
      print(line, end=' ') 

我想要做的是使代碼按字母順序排序保存到單獨的文本文件的結果。 我仍然得到一個錯誤的東西關於行沒有被定義,所以有什麼毛病我的整個代碼,謝謝你的任何幫助。 這是錯誤消息: 回溯(最近通話最後一個):代碼不工作,出現語法錯誤

File "E:\GCSE COMPUTING\task 3 trial.py", line 41, in

print(line, end=' ') 

NameError:名字 '線' 沒有定義

+2

'withoopen'應該是'open'。你錯過了之間的空間。 – cezar

+0

謝謝你生病了試試這個 – shakif

+0

你應該總是分享你的錯誤。我們可以非常快速地閱讀這些語法錯誤。你也應該仔細閱讀錯誤,他們通常會告訴你什麼是錯的。 – OYRM

回答

0

這是你的錯誤:

withopen('class1.txt', 'r') as r: 
    print(line, end=' ') 

您應該使用with open(file, mode) as r:,如下面的代碼:

viewscores= input("Please select a class from 1,2 or 3 and press space and choose one from alphabetically, average or highest?") 
if viewscores=='1 alphabetically':  
    with open('class1.txt', 'r') as r: 
     print(line, end=' ') 

閱讀docs