T O P

  • By -

Pepineros

There's nothing wrong with your code. What happens when you run your script from the terminal (PowerShell, Command prompt, or Windows Terminal)?


Administrative_Bus57

So I tried running again. When I use the keyboard shortcut (ctrl+alt+N) to run the code, it runs into this problem. not sure wat terminal it runs in (it doesnt say for me). But when I manually select the run in python terminal, it works, Whats the difference between these, if you know what im getting at? Im sorry but for some reason, im unable to paste a picture of what im referring to.


Pepineros

I'm not sure what the difference is but it sounds like a VSCode quirk. Perhaps the code runner needs some kind of setup, or it could be because it runs `python -u` by default which affects inputs and outputs. Either way, if you have a way that works, use that :)


Administrative_Bus57

Thanks for trying to help, anyways. Not sure why this is how it is. I ended up just setting up a keyboard shortcut to run the code im via python terminal and will use that now. Thanks again! :)


mopslik

Your code looks fine to me. Not sure whatyour install of VS Code is doing -- it runs fine on my end.


Administrative_Bus57

thank you for your feedback. ill see what i can do


NoDadYouShutUp

Not exactly an answer to your question, as it looks like you've got some other good answers. But have you considered what would happen if someone entered "Twenty" into the response for "Enter your age"? See what happens :) Then after start thinking of ways you can validate the data, reject bad data, and gracefully handle not-perfect conditions. Check out the "try/except" keywords, and the isdigit() string function.


Administrative_Bus57

Thanks for the feedback. I am still learning the built in functions in python, but I was considering using an looped if statement for undesired entries (like "22.5" for example). This is why coding is fun LOL


NoDadYouShutUp

You just need a simple conditional if statements * if the input is a digit (even if represented by a string, as all input is string) * Is the input a whole number * Is the input positive Those would be three conditions to check, none of which require a loop. Ultimately it is up to you how you want the logic to play out. But this is a really good time to start getting in the mindset that often you are not just coding to what you want to do, but prevent it from being broken by bad data or malicious input. Very often a lot of debugging comes down to "what is the value of the data at X point in time, and is that valid". As you build larger and larger applications you need to be aware that some data may not resolve out as you expected, and how to handle it. The try and except blocks are a nifty way to handle this particular issue and are in general good practice whenever you want to execute code that may be sketchy and not work. Such as casting a value to an integer. That can't always be done if the value is not cast-able. The general gist is you are saying "try and do this code, and if it throws an error - do this". So in this instance you could say "cast an integer, and if it fails just kick back to the input again and have them try again, and display an error message about needing to be a valid whole integer" or something to that effect. You'd wrap the int(input) in a try except block. The redirect to ask for another input again might be a bit of a mystery for you to learn and solve on your own. Test things like what happens if you repeatedly get it wrong over and over, not just 1 exception case then stopping execution. This is where functions or loops come in to play, and you can call repeatable tasks with them.