I was taking a computer science class, and I was tasked to create a program that implemented a function which took parameters. My teacher told me that the following code is not a function leaving me confused as to what is necessary for some lines of code to be classified as a “function”.
def game(numbers, max_turns, pfl, tgl):
turns=0
flag=False
print("You have "+str(max_turns)+" turns to guess the number. Let's start!")
while turns<max_turns:
guess=int(input("Enter your guess: "))
if guess==numbers:
pfl.append("Passed")
tgl.append(max_turns)
flag=True
print("You got it right! Try to make it a little harder!")
break
elif guess<numbers:
print("Higher")
turns=turns+1
else:
print("Lower")
turns=turns+1
if flag==False:
pfl.append("Failed")
tgl.append(max_turns)
print("You failed! Maybe give yourself more chances next time? The number was "+str(number))
I had also been able to call it like any normal function many times in the program:
game(number, turn, passfail, turnsgiven)
game(number, randomturn, passfail, turnsgiven)
I assumed that this was a function as I defined it as one and was able to call it like any normal function wherever necessary, but my teacher said this was not a function.
Eesh Pant is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.