Question
This program will prompt the user for a positive integer n and then display the nth Fibonacci number. TypeError: can only concatenate str (not int)
This program will prompt the user for a positive integer n and then display the nth Fibonacci number.
TypeError: can only concatenate str (not "int") to str
Can someone explain why ?
def get_num():
#prompt user for a number
n = input('Which Fibonacci number would you like to see? ')
#Negetive numbers are not accepted.
assert n.isnumeric(), "Number has to be positive! "
return n
def fib_bottom_up(n):
if n == 1 or n == 2:
return 1
bottom_up = [None] * (n + 1)
bottom_up[1] = 1
bottom_up[2] = 1
for i in range(3,n+1):
bottom_up[i] = bottom_up[i-1]+bottom_up[i-2]
return bottom_up[n]
x = get_num()
print(fib_bottom_up(x))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started