Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to compare a int variable with a function that returns a int

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading