scorematch_A = [0,1,2,3,4,5,6,7,8] import random random.shuffle(scorematch_A) current = scorematch_A.pop() score = current print('Результат матча: %d ' %score ) com1 = open('text.txt', 'w') text.write('%d', %current ) 1 answer
You incorrectly use
%to pass the value of a variable to a function.According to PEP8 :
You can also use the
%operation to format strings. It interprets the left operand as asprintf-style formatting string, which should be applied to the right-hand operand, and returns the string resulting from this conversion.More details about this in response to ruSO.
In your case,
print('Результат матча: %d ' % score )andtext.write('%d' % current )will be correct.And also incorrectly write data to the file. Instead
com1 = open('text.txt', 'w') text.write('%d', %current )it will be right:
with open('text.txt', 'w') as file: file.write('%d' % current )
But in general, as mentioned correctly in the comments , read more Python guides and practice more to understand what is happening in the code.
- Traceback (most recent call last): File "C: \ Users \ SAMURAI \ Desktop \ 122.py", line 10, in <module> text.write ('% d'% current) NameError: name 'text' is not defined - ADDO BOSS
- @ADDOBOSS you really have not created such a variable. - andreymal
- So this is not a variable, but the name of a text file - ADDO BOSS
- @ADDOBOSS then you do not understand the essence of programming and variables, and it remains only to advise you to re-read some python textbook. You are in the last line of the program trying to access the text variable, which you have not created anywhere. - andreymal
- oneYou can also use
file.write(str(score))orprint('Результат матча:' , score, file=file)instead of'%d' % score- jfs
score = random.choose(scorematch_A)instead ofshuffle()+.pop()use - jfs