import random
import time
count=0
counter=0
score=0
function=['+','x','-']
print('Welcome To The Arithmetic Quiz!')
name=input('Please enter you name:')
print('Thanks' , name , 'Lets Get Started!')
time.sleep(1)
while counter <10:
firstnumber=random.randint(0,12)
secondnumber=random.randint(0,6)
operator=random.choice(function)
question=print(firstnumber, operator, secondnumber, '=')
Answer = input('Answer:')
if operator== '+':
count=firstnumber+secondnumber
if count == int (Answer):
print ('Correct!')
score= score+1
else:
print ('Incorrect!')
elif operator== 'x':
count=firstnumber*secondnumber
if count == int (Answer):
print ('Correct!')
score= score+1
else:
print ('Incorrect!')
elif operator== '-':
count=firstnumber-secondnumber
if count == int (Answer):
print ('Correct!')
score= score+1
else:
print ('Incorrect!')
counter += 1
time.sleep(0.5)
print('Well Done' ,name,'! You Scored',score ,'Out of 10!')
f = open("Test Scores.txt","a")
f.write(name)
f.write(' ')
f.write('%d' % score)
f.close()
代码的最后一部分应该在文本文件中写下该人的姓名和分数。 它完美地做到了这一点,但我希望每次都写一个新的行(不像现在这样在同一行上)。
答案 0 :(得分:0)
使用:
f.write(name + '\n')
而不是
f.write(name)
答案 1 :(得分:0)
写完内容后,可以在最后插入换行符\n
。它会为你插入一个新行。
f.write(something + '\n') # Adds a new line at the end
答案 2 :(得分:0)
你也可以做一个定义来写一个文件。
def writeFile(value):
open('file.txt', 'a') as file
file.write(value)
file.write('\n')
要使用它,您只需要调用此定义。
你写的所有内容都会以新的方式结束,因为&#39; \ n&#39;。
writeFile('HelloWorld')