2017-08-24 66 views
0
import random # imports the random module 

user_class = "" # Global variable 
question_counter = int(0) # sets counter 
amount_correct = int(0) # sets score 



possible_operators = ("+","-","x") # creates a list of operators 

while user_class == "": # Indefinite iteration - Runs until a valid class is given 
    user_class = str(input("Please enter your class")) 

    if user_class == "Rainbow": # Testing variable class - Will not crash the program 
     class_file = open("RainbowClass.txt","a") 

    elif user_class == "Sun": 
     class_file = open("SunClass.text","a") 

    elif user_class == "Moon": 
     class_file = open("MoonClass.text","a") 

    else: 
     print("Not quite. Try again:") 
     user_class = "" 



name = str(input("Please enter your name")) 


while question_counter < 10: # Indeffinate iteration – number of questions 

    num1 = random.randint(1,10) # random number generation 
    num2 = random.randint(1,10) 
    operator = random.choice(possible_operators) # Chooses one of the operators from the list 

    if operator == "+": 
     answer = num1 + num2 
    elif operator == "-": 
     answer = num1 - num2 
    elif operator == "x": 
     answer = num1 * num2 

    print("What is the answer to ", num1," "+operator+"" ,num2,"?") 
    user_answer = int(input()) 


    if user_answer == answer: 
     question_counter = question_counter + 1 
     amount_correct = amount_correct + 1 
     print("Correct!") 

    else: 
     print("Incorrect") 
     question_counter = question_counter + 1 


final_score = amount_correct 
print("Well Done! You scored",amount_correct,"out of",question_counter,".") 


amount_correct = str(amount_correct) 
class_file.write(user_class) 
class_file.write(name) 
class_file.write(amount_correct + "\n") 

class_file.close 

数据这是我的代码。当我运行它时,它不会收到任何错误,但我正在尝试获取用户的类并根据输入打开文本文件。文件处于打开状态,但当涉及到底部时,它不会写入我想要的变量内部的测验数据。写入文件中使用从变量

如何解决这个?在此

回答

0

未经测试的裂纹(对不起,Python是另一个系统上)。伊利诺伊州把它分解成块:

import random 

# Got rid of your global 'user_class' 
# Moved the counter(s) to local function variables  
# Moved the operator options to local function variables 

我们解决您的第一while,我会做:

def get_class_file(): 
    while True: 
     # Don't need to call str(); input() returns a 'str' 
     user_class = input("Please enter your class: ") 

     if user_class == "Rainbow": 
      class_file = "RainbowClass.txt" 
      break 
     elif user_class == "Sun": 
      class_file = "SunClass.txt" 
      break 
     elif user_class == "Moon": 
      class_file = "MoonClass.txt" 
      break 
     else: 
      print("Not quite. Try again...") 

    return class_file 

保持这样的:

# Don't need to call str(); input() returns a 'str' 
name = input("Please enter your name: ") 

现在第二while,类似的功能:

def get_score(): 
    # Don't need to call int() 
    question_counter = 0 
    amount_correct = 0 
    # Make this a tuple sequence; they are immutable 
    possible_operators = ("+","-","x",) 

    while True: 
     if question_counter < 10: 
      num1 = random.randint(1,10) # random number generation 
      num2 = random.randint(1,10) 
      operator = random.choice(possible_operators) 

      if operator == "+": 
       answer = num1 + num2 
      elif operator == "-": 
       answer = num1 - num2 
      elif operator == "x": 
       answer = num1 * num2 

      user_answer = int(input("What is the answer to: " 
            + str(num1) + operator + str(num2) + "?\n")) 

      # Take out of the if/else since we always increment 
      question_counter += 1 

      if user_answer == answer: 
       amount_correct += 1 
       print("Correct!") 
      else: 
       print("Incorrect!") 

     else: 
      break 

    return amount_correct 

让我们把它放在一起,看看我们如何得到的变量和如何将它们写入文件:

import random 

def get_class_file(): 
    while True: 
     # Don't need to call str(); input() returns a 'str' 
     user_class = input("Please enter your class: ") 

     if user_class == "Rainbow": 
      class_file = "RainbowClass.txt" 
      break 
     elif user_class == "Sun": 
      class_file = "SunClass.txt" 
      break 
     elif user_class == "Moon": 
      class_file = "MoonClass.txt" 
      break 
     else: 
      print("Not quite. Try again...") 

    return class_file, user_class 


def get_score(): 
    # Don't need to call int() 
    question_counter = 0 
    amount_correct = 0 
    # Make this a tuple sequence; they are immutable 
    possible_operators = ("+","-","x",) 

    while True: 
     if question_counter < 10: 
      num1 = random.randint(1,10) # random number generation 
      num2 = random.randint(1,10) 
      operator = random.choice(possible_operators) 

      if operator == "+": 
       answer = num1 + num2 
      elif operator == "-": 
       answer = num1 - num2 
      elif operator == "x": 
       answer = num1 * num2 

      user_answer = int(input("What is the answer to: " 
            + str(num1) + operator + str(num2) + "?\n")) 

      # Take out of the if/else since we always increment 
      question_counter += 1 

      if user_answer == answer: 
       amount_correct += 1 
       print("Correct!") 
      else: 
       print("Incorrect!") 

     else: 
      break 

    return amount_correct, question_counter 

class_file, user_class = get_class_file() 
name = input("Please enter your name: ") 
final_score, num_asked = get_score() 


print("Well Done!\n" 
     "You scored " + str(final_score) + " out of " + str(num_asked) + ".") 

# In all methods you called 'a' mode so set that here 
# Using 'with open()' automatically closes the file after completion 
with open(class_file, 'a') as f: 
    f.write(user_class + "\n") 
    f.write(name + "\n") 
    f.write(str(final_score) + "\n") 

让我知道,如果发生了任何事情,错误和这样的。再次,无法测试。

+0

嘿pstatix,谢谢您的回答。一切运行良好,直到74行,这条语句:f.write(final_score +“\ n”) 。它返回一个回溯错误。这是贝说什么:回溯(最近通话最后一个): 文件 “//文件服务器/学生/ 11197/GCSE CS评估夏/任务2 /堆栈溢出answer.py”,行73,在 f.write (final_score + “\ n” 个) 类型错误:不支持的操作数类型(一个或多个)为+: 'INT' 和 'STR' >>> –

+0

FIXED: 'f.write(STR(final_score)+ “\ n”)' ...谢谢@pstatix –

+0

@BenColmer对不起,所有那些简单的失败!我已更新它以包括该修复。再次,我无法测试我写作的时间。但问题仍然存在,你明白做了什么?如果你这样做,请选择作为答案,它适用于你! – pstatix