How to compare a int variable with a function that returns a int
Question:
I’m trying to make code( that I stole) output the Fibonacci sequence and I’m having trouble letting the function Terms be able to compare to an variable. I know this is messy code but I like 3 days into python so I don’t know what to do.
# first two terms
n1, n2 = 0, 1
count = 0
# Asks amount of terms to be generated
def Terms():
while True:
nterms = (input("How many terms? "))
if nterms.isdigit():
nterms= int(nterms)
if nterms > 0:
break
else:
print("0")
else:
print("Please choose an number that is able to be used")
return nterms
Terms()
# Calls Terms then generates
print(int(Terms))
def Fibonacci():
# first two terms
n1, n2 = 0, 1
count = 0
Terms()
print("Fibonacci sequence:")
while count < Terms: #Problem right here TypeError: int() argument must be a string, a bytes-like object or a real number, not 'function'
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Fibonacci()
I tried making terms an int but it still doesn’t work
Answers:
The fix is probably
while count < Terms():
The error message tells you explicitly that you may not compare to a function (but to a function result). (But after this fix, you will run into another problem…)
An alternative implementation:
# Query the number of terms
while True:
try:
n = int(input("How many terms [integer > 1] ? "))
if n > 1:
break
except:
pass
print("Invalid input, try again")
# Output the desired number of terms
f0, f1= 0, 1
for i in range(n):
print("F"+str(i)+" =", f0)
f0, f1= f1, f0+f1
There were a few small things. So you called the different functions from a few places. So I removed a few calls and put returnvalue from Terms() in variable terms, and then it worked.
# first two terms
n1, n2 = 0, 1
count = 0
# Asks amount of terms to be generated
def Terms():
while True:
nterms = (input("How many terms? "))
if nterms.isdigit():
nterms= int(nterms)
if nterms > 0:
break
else:
print("0")
else:
print("Please choose an number that is able to be used")
return nterms
# Calls Terms then generates
def Fibonacci():
# first two terms
n1, n2 = 0, 1
count = 0
terms = Terms() # This is the new variable
print("Fibonacci sequence:")
while count < terms: # And using the variable here
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Fibonacci()
I’m trying to make code( that I stole) output the Fibonacci sequence and I’m having trouble letting the function Terms be able to compare to an variable. I know this is messy code but I like 3 days into python so I don’t know what to do.
# first two terms
n1, n2 = 0, 1
count = 0
# Asks amount of terms to be generated
def Terms():
while True:
nterms = (input("How many terms? "))
if nterms.isdigit():
nterms= int(nterms)
if nterms > 0:
break
else:
print("0")
else:
print("Please choose an number that is able to be used")
return nterms
Terms()
# Calls Terms then generates
print(int(Terms))
def Fibonacci():
# first two terms
n1, n2 = 0, 1
count = 0
Terms()
print("Fibonacci sequence:")
while count < Terms: #Problem right here TypeError: int() argument must be a string, a bytes-like object or a real number, not 'function'
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Fibonacci()
I tried making terms an int but it still doesn’t work
The fix is probably
while count < Terms():
The error message tells you explicitly that you may not compare to a function (but to a function result). (But after this fix, you will run into another problem…)
An alternative implementation:
# Query the number of terms
while True:
try:
n = int(input("How many terms [integer > 1] ? "))
if n > 1:
break
except:
pass
print("Invalid input, try again")
# Output the desired number of terms
f0, f1= 0, 1
for i in range(n):
print("F"+str(i)+" =", f0)
f0, f1= f1, f0+f1
There were a few small things. So you called the different functions from a few places. So I removed a few calls and put returnvalue from Terms() in variable terms, and then it worked.
# first two terms
n1, n2 = 0, 1
count = 0
# Asks amount of terms to be generated
def Terms():
while True:
nterms = (input("How many terms? "))
if nterms.isdigit():
nterms= int(nterms)
if nterms > 0:
break
else:
print("0")
else:
print("Please choose an number that is able to be used")
return nterms
# Calls Terms then generates
def Fibonacci():
# first two terms
n1, n2 = 0, 1
count = 0
terms = Terms() # This is the new variable
print("Fibonacci sequence:")
while count < terms: # And using the variable here
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Fibonacci()