Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3323820
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:20:52+00:00 2026-05-17T23:20:52+00:00

I’m completely new to Python and I’ve been trying to make a fibonacci program

  • 0

I’m completely new to Python and I’ve been trying to make a fibonacci program with it.

def fib(n):
    print 'n =', n
    if n > 1:
        return n * fib(n - 1)
    else:
        print 'end of the line'
    return 1

n = raw_input('Input number: ')
int(n)
fib(n)

When I try to run this program, I get the following error after entering the number:

Input number: 5

n = 5

Traceback (most recent call last):

File “fibonacci.py”, line 11, in

fib(n)

File “fibonacci.py”, line 4, in fib

return n * fib(n - 1)

TypeError: unsupported operand type(s)
for -: ‘str’ and ‘int’

If I run the interpreter and import just the function (without the code after it), supply the value for n and call the function with the value as the parameter, it works.

I tried converting the input to int since I thought it was a string problem but no dice. I don’t really know where I went wrong so if you could please shed some light on the subject, it’ll be much appreciated.

I’d love to change the problem title to something specific but I don’t really know what the problem is.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:20:53+00:00Added an answer on May 17, 2026 at 11:20 pm

    The problem is that raw_input is providing a string, not an integer. Can I suggest just putting the integer value into n in the first place:

    n = int(raw_input('Input numver: '))
    fib(n)
    

    Avoid n = int(n) as in a longer section of code it would be unclear when you came back to it what type n is, and you have no need for the original string value.

    The deeper understanding you need is that Python is strongly typed – it cares what type everything is, but it is also dynamically typed, so n could change from holding a string value to holding an integer value. But Python is still keeping track of what is held, so when you put the return value of raw_input into n, Python knows it is a string, so multiplying that string by a number makes no sense and returns an error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.