Just to echo what others have said, Dundee is the best option. As well as the budget hotels there are some nicer mid-tier hotels. The Apex is decent and there's now a Malmaison just opened in the last 6 months. Both of them are within sight of the Tay Road Bridge and train station, it makes travel to St Andrews very easy. But be warned, Dundee also feels the accommodation pressure of The Open week so prices do go up. Book early, I guess.
Contrary to perceived opinion, Dundee is quite a nice city. Yes it's small and has its rough bits, but it's definitely not the worst place I've lived in the UK.