-2
import random

x = 1

z = random.randint(0,255)

if x != z:
    print('unsuccessful')
    
if x = z:
    
    print('successful')

    f = open("C:\Users\AsusTUF\ai.txt", "a")
    del = text.replace('z = random.randint(0,255)','z = '

So i want to write value of z into the file, but I dont have an idea on how to do it! Thanks in advance for the help.

Keep in mind I am new to python so I tried return z but did not even run that because i know it wont work.

Péter
  • 1
  • 2
    `f.write(str(z))` writes z to the file. – Mislah Jan 19 '23 at 17:37
  • Im confusioned. Whats the input file look as like, currently? and then what is the desired or result output? – rv.kvetch Jan 19 '23 at 17:37
  • note here, the `if x != z:`, then in latter case don't need a `if x = z:` - this is a good case for an `else:` actually. – rv.kvetch Jan 19 '23 at 17:38
  • Since `x` equals 1, and `z` equals `x`, you could just directly write "1" into the file. Although that is probably not the essence of your question. – 9769953 Jan 19 '23 at 17:38
  • Don't use [`del`](https://stackoverflow.com/questions/6146963/when-is-del-useful-in-python) as variable name. – Ignatius Reilly Jan 19 '23 at 17:39
  • Replace the last line with `f.write(f'z = random.randint(0,255), z = {z}\n')`. And don't forget to close the file afterwards. – 9769953 Jan 19 '23 at 17:39
  • But is that the line in your file? `z = random.randint(0,255)` ? That doesnt make sense, why it would be in a file (the string value i mean). – rv.kvetch Jan 19 '23 at 17:41
  • 1
    Probably you want to use `if x == z:` (with two equals). – Ignatius Reilly Jan 19 '23 at 17:41
  • Try `f.write(str(z))` instead of the the line `del = tex....` to write z to the file. Btw, there is only 1/256 probability for x to be equal to z. Also check if you meant to use `'x == z'` in the if statement. – Mislah Jan 19 '23 at 17:45
  • `text` is not defined anywhere in your code. Are you trying to write `z` in a string or in a text file? There're many ways you can [format strings](https://realpython.com/python-f-strings). Python [documentation](https://docs.python.org/3/library/string.html) is always a good place to start. – Ignatius Reilly Jan 19 '23 at 17:45
  • TBH, that the line: `del = ...` : it *won't work* anyway. Not run the code, I mean. – rv.kvetch Jan 19 '23 at 17:49

3 Answers3

0

The function you are looking for is f.write('text'). For example:

import random

x = 1

z = random.randint(0,255)

if x != z:
    print('unsuccessful')
    
if x == z:
    
    print('successful')

    f = open("C:\Users\AsusTUF\ai.txt", "a")
    f.write(str(z))
    f.close()

Also it's not a good idea to use del as a variable name once it is a keyword from the language.

gustavo_ek
  • 31
  • 3
0

The line being as such:

del = ...

It 'll not actually work.

Here is the repro example using a Python REPL:

py
zsh: command not found: py
python
Python 3.8.14 (default, Nov  7 2022, 11:52:33) 
[Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> del = ...
  File "<stdin>", line 1
    del = ...
        ^
SyntaxError: invalid syntax

Even other operator, being as such in:

>>> in = ...
  File "<stdin>", line 1
    in = ...
    ^
SyntaxError: invalid syntax

Rename it, then We All Good ™️:

>>> IN = ...
>>> IN
Ellipsis
rv.kvetch
  • 9,940
  • 3
  • 24
  • 53
0

Apart from other answers, I assume that you are trying to check how many runs it takes to get the random value equals to x.

It can easily be done by adding a while loop with a counter.

import random

x = 1
count = 0  # Counter
while True:  # Loop indefinitely
    z = random.randint(0, 255)  # Generate random number
    count += 1  # Increasing the counter

    if x != z:
        print('Unsuccessful: %d' % count)
    else:
        print('Successful: %d' % count)
        f = open("i.txt", "a")  # You can also give relative path here
        f.write(str(z))  # Writing the value to file
        break  # Breaking the loop
Mislah
  • 318
  • 1
  • 2
  • 11