![](/img/trans.png)
[英]python “unicodescape error” when i try to make acending numbers with string
[英]Python: Saving numbers error when saved as string
我的代碼有問題,在這里:
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的數字(例如8),但打印到文件的數字始終為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
猜猜我將發布整個代碼以供參考:
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
將分數寫入文件后,您正在運行mainLoop
,因此該文件將不包含正確的分數。 只需將詢問問題的代碼移動10遍( while count < 10
等等)到將分數寫到grade_book
的代碼grade_book
( if name in grade_book.keys():
等等)。
您可能會發現避免使用全局變量會有所幫助。 取而代之的是,您可以具有一個根據用戶是否正確響應而返回True
或False
的問題函數,然后我們僅使用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))
在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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.