Python:保存为字符串时保存数字错误

时间:2015-10-17 13:42:45

标签: python

我的代码存在问题,请点击此处:

correct = 0

grade_book = {}
File = open('Test.txt', 'r')
for line in File:
     name, scores = line.split(':')
     grade_book[name] = scores.strip()
File.close()
print(grade_book)

name = input("Name: ")
if name in grade_book.keys():
    grade_book[name] += ',' + correct
else:
    grade_book[name] = correct

File = open('Test.txt', 'w')
for name, scores in grade_book.items():
    out_line = str(name) + ':' + str(scores) + "\n"
    File.write(out_line)

File.close()

问题是它给出了一个错误说明:

  

TypeError:无法转换' int'隐含地反对str

当程序尝试保存'正确'到文件中的现有名称。我已尝试使用以下方法解决问题:

    name = input("Name: ")
if name in grade_book.keys():
    grade_book[name] += ',' + str(correct)
else:
    grade_book[name] = correct

但问题是,打印到文件的数字始终为0,尽管'正确'分配给大于0的数字,例如8.另一方面,它会像以前一样给出错误,只是上面的问题。

对此有何解决方案?

  

是的,我可能错过了一些非常明显的东西。

代码'更正':

def mainLoop():
    global count
    global correct
    Num1 = randint(1, 10)
    Num2 = randint(1,10)
    Operand= randint(1,3)

    if Operand == 1:
        question = str(Num1) + " + " + str(Num2) + " = "
        answer = Num1 + Num2
    elif Operand == 2:
        question = str(Num1) + " - " + str(Num2) +" = "
        answer = Num1 - Num2
    else:
        question = str(Num1) + " x " + str(Num2) + " = "
        answer = Num1 * Num2

    userAnswer = int(input(question))
    if userAnswer == answer:
        correct += 1

猜猜我会发布整个代码以供参考:

from random import randint
import time
count = 0
correct = 0

def mainLoop():
   global count
   global correct
   Num1 = randint(1, 10)
   Num2 = randint(1,10)
   Operand= randint(1,3)

   if Operand == 1:
       question = str(Num1) + " + " + str(Num2) + " = "
        answer = Num1 + Num2
   elif Operand == 2:
       question = str(Num1) + " - " + str(Num2) +" = "
       answer = Num1 - Num2
   else:
       question = str(Num1) + " x " + str(Num2) + " = "
       answer = Num1 * Num2

 userAnswer = int(input(question))
 if userAnswer == answer:
  correct += 1



grade_book = {}
File = open('Test.txt', 'r')
for line in File:
    name, scores = line.split(':')
   grade_book[name] = scores.strip()
File.close()
print(grade_book)

name = input("Name: ")
if name in grade_book.keys():
   grade_book[name] += ',' + str(correct)
else:
   grade_book[name] = str(correct)

File = open('Test.txt', 'w')
for name, scores in grade_book.items():
    out_line = str(name) + ':' + str(scores) + "\n"
    File.write(out_line)

File.close()

 while count < 10:
    mainLoop()
    count += 1
  

答应是否快速,可能是错误的

文本文件示例: Test:1,5 John:1,0

2 个答案:

答案 0 :(得分:1)

在将分数写入文件后,您正在运行mainLoop,因此该文件将不包含正确的分数。只需将问题10次(while count < 10等)的代码移动到将分数写入grade_bookif name in grade_book.keys():等)的代码之上。

您可能会发现避免使用全局变量会很有帮助。相反,您可以使用问题函数返回TrueFalse,具体取决于用户是否正确响应,然后我们只使用sum来计算正确和错误的响应。

from random import randint
import time

def question():
   a = randint(1, 10)
   b = randint(1,10)
   operand= randint(1,3)

   if operand == 1:
       sign = '+'
       answer = a + b
   elif operand == 2:
       sign = '-'
       answer = a - b
   else:
       sign = 'x'
       answer = a * b

   user_answer = int(input('{} {} {} = '.format(a, sign, b)))
   return user_answer == answer # returns True if correct, False if not

grade_book = {}
with open('Test.txt', 'r') as file:
    for line in file:
        name, scores = line.split(':')
        grade_book[name] = scores.strip()
print(grade_book)

name = input("Name: ")

# Ask a question 10 times and sum up the correct responses
# (This works because sum counts True as 1 and False as 0)
correct = sum(question() for _ in range(10)) 

if name in grade_book.keys():
   grade_book[name] += ',' + str(correct)
else:
   grade_book[name] = str(correct)

with open('Test.txt', 'w') as file:
    for name, scores in grade_book.items():
        file.write('{}:{}\n'.format(name, scores))

答案 1 :(得分:0)

grade_book[name] = correct中您指定整数值correct。所以它应该是:

name = input("Name: ")
if name in grade_book.keys():
    grade_book[name] += ',' + str(correct)
else:
    grade_book[name] = str(correct)

你没有做任何“正确”的事情。它总是0。