Skip to content
Advertisement

Need help to make while loop restart my Python program from top

I would like to get help with this program I have made. The function of the code is so the users can input a city anywhere in the world and they will then get data about the weather for that city.

I want it to restart the program from the top, but it only restarts the program from where the results are.

JavaScript

So this is what happens.. The loop only loops the question with the input and data already filled in.

JavaScript

I want the code to loop from the top so I can press y so the program will ask me for another city to input.

Advertisement

Answer

You are never updating your values. Let’s take a simpler example:

JavaScript

If I run this, I will get x printed out forever if I choose, say 5. The code defining x will never be re-run because it’s outside of the loop. To fix this, I can move my code for x into the while loop:

JavaScript

This will run the code for x every time the loop executes, so x can now change. Applying this to your code:

JavaScript

Now the value for City can change, and you can apply that to the other data structures as well

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement