i am new to python and i am messing around with it i want to know why isnt this program working?

Question:

i am messing around with custom functions but i cant make it work and i dont know how to make it work, i searched around a bit but still nothing seems to solve this problem.
i would be grateful if someone helped 🙂

i wanted to try making the random.randint() function shorter into r()
and thank you for helping me out.

i realized my mistake, i saw people using open() read/write commands i thought i understood how it worked, guess i thought wrong.

thank you <3

my code:

import random as ra

def r(a,b):
    with open(a,b) as int:
        r = ra.randint()
    return

w = r(1,100)


print(w)

compiler says

line 8, in <module>
    w = r(1,100)

line 4, in r
    with open(a,b) as int:


TypeError: open() argument 'mode' must be str, not int
Asked By: Alwma2

||

Answers:

open() function is used to open files, with the first parameter being a file path and the second parameter being a ‘mode’, typically ‘r’ for read, or ‘w’ for write.
https://docs.python.org/3/library/functions.html#open

Answered By: girishsenthil

The line with open(a,b) as int: doesn’t really make sense. open is used for reading files and int is a datatype.

Can you clarify what the function r is supposed to do? This would help us help you.

Answered By: ryerrabelli

The open function takes 2 positional arguments, The file and a mode, An example being :

with open("C:/Users/Alwma2/Documents/myfile.txt", "r") as file
    my_file = file.read()

The mode is telling the file system what you want to do in this case, In this example we used "r" to tell it that we will be reading from it, Other modes include "rw", "w", "b" and more.

Answered By: tarnished

Hey dont know why you want to use the open method
in your function you declare the variable and use return to get the value
since you are new to this ChatGPT is really helpful when it comes to easy stuff like this

import random as ra

def r(a,b):

    random_int = ra.randint(a,b)
    return random_int

w = r(1,100)


print(w)
Answered By: StbE
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.