r/PythonLearning • • Jul 11 '25

Code won't print

Post image

What's the error?(I'm a newbie)

115 Upvotes

86 comments sorted by

8

u/Character-Comfort539 Jul 11 '25

4

u/Ill-Diet-7719 Jul 11 '25

Could there be a problem with visual studio?(I'm doin on it and everything's been fine)

4

u/Character-Comfort539 Jul 11 '25

There could be, IDE's can be a bit squirrelly to setup if you're not familiar with them and are new. I'd add something like `sdjflasjdfasjd` to the bottom of the file and see if the interpreter throws an error. Your other comments say "it doesn't print anything at all" but I'm not convinced anything is even running to begin with. Throwing a nonsense line into the file should give you an error. Just to be sure, save the file as like odd.py and run what's below in the terminal and let us know how it goes

python odd.py

4

u/undernutbutthut Jul 12 '25

Remember to save the code first then run it.

2

u/lusvd Jul 12 '25

just in case keep in mind that Visual Studio != vscode

2

u/ofSkyDays Jul 14 '25

If you have it often open and constantly sleep/wake pc, it could have developed a glitch. Idk it always happens whenever I do simple web dev stuff.

One time I had horizontal scrolling come out of nowhere, and I could it find the issue and nothing pointed to there being anything, but a clean shutdown and restart of vscode fixed it.

2

u/SizzlingSquigg Jul 11 '25

I appreciate the link with test.

OP, you can simplify, too. See https://www.online-python.com/cjIfx1YLwN

2

u/number-13 Jul 11 '25

Nice one bro.. I'm a newbie as wel

6

u/GrumpMadillo Jul 11 '25

Why won't it print? We need more details. Code looks like it should work

1

u/Ill-Diet-7719 Jul 11 '25

Should I send the output?

4

u/GrumpMadillo Jul 11 '25

It looks like based on other comments its compiling, but you aren't getting an output. Are you looking in the right place in visual studio? Which output window are you looking at? The more information you share the better people will be able to help you

3

u/Ill-Diet-7719 Jul 11 '25

Ok it was indeed a problem with vs. I ran the code again and it's working. I don't know why it wasn't back then. Thank you all for taking your time out helping me out. You guys are the bestđŸ«‚đŸ«‚

3

u/EmuBeautiful1172 Jul 11 '25

Codewith.mu is a good option for an IDE to write code and practice for beginners

1

u/Ill-Diet-7719 Jul 11 '25

Visual studio? Is that bad?

3

u/EmuBeautiful1172 Jul 11 '25

No it’s not bad actually visual studio is used by a lot of programmers. But it has a lot of extra features and extensions that could pop up and make things confusing in the beginning of coding. But if you’re comfortable with that and like it then use it. For practicing good syntax and beginner projects a basic style IDE will be a lot easier to view what was written and to run it easy. I like codewith.mu because it’s literally type it in and run it and of course it has the necessary features like what errors you may have and predictions on a minimal scale. When I come across an error and can’t figure it out on my own. I go to the line where the error is. The IDE will tell you that and then I copy that specific “paragraph of code and ask chat gpt to explain what’s going on.

It’s also good to look up definitions and reasons to what each part of the code is doing. Instead of just typing it in and expecting a result.

1

u/Ill-Diet-7719 Jul 12 '25

IDE is the one which is pre installed with python right? Yeah there're lot many extensions vs is suggesting. What exactly are those? Necessary? And your suggestions if any?

2

u/droopy227 Jul 12 '25

It’s very mid for python. You can use whatever you like but I’d say give some other editors a shot and see how you feel. I used to use VS a lot since that’s where I learned C# and C++ but I eventually realized unless you need the features it provides you could just use something much lighter and easier to use.

1

u/Ill-Diet-7719 Jul 12 '25

There're lot of extension vs is suggesting. Should I download em?

1

u/droopy227 Jul 12 '25

That’s what I mean. For python and especially for beginners, most of those extensions aren’t that useful. You can always go back and install them when you need but it’s more of a “install it when your project needs it” kinda thing. Also with python most of the time you’re just using pip to install packages and importing them so VS doesn’t really add much that you can’t already figure out yourself.

3

u/blacksmithshands Jul 11 '25

you could try pycharm as well. it's what i used in school, and it was pretty nice. vscode works well too, though.

As for your output not working, did you remember to save your file before running it?

1

u/RealLifeRiley Jul 12 '25

VS code is fine. I became a neovim convert, but it’s a super common editor.

4

u/D3str0yTh1ngs Jul 11 '25

Well.. What exactly is the result of running it? (also did you remember to save the file?)

1

u/Ill-Diet-7719 Jul 11 '25

I'm running it on visual studio. There's a terminal right there. And it's printing nothing except for file location (vs starts any code with file location and then the output).

2

u/Imaginary-Survey8769 Jul 12 '25

Op sometimes vs code give you unexpected error just can't compile or something close vs code and reopen it.... Or shutdown pc if it works....

2

u/Ill-Diet-7719 Jul 12 '25

Yeah everything was fine. This was first time and I panicked lmao. Thank you anyways

1

u/mvstartdevnull Jul 11 '25

Assuming your intent is to only print uneven I, code looks fine to me. How are you executing? Any error messages? 

1

u/Ill-Diet-7719 Jul 11 '25

It's printing nothing. Like nothing. Blank

1

u/WillingSupp Jul 11 '25

have you saved? How did you run it?

1

u/Ill-Diet-7719 Jul 11 '25

I'm doin it on vs. so just running it on its terminal

1

u/xX_fortniteKing09_Xx Jul 11 '25

You still have to save the file

1

u/EmuBeautiful1172 Jul 11 '25

It should tell you the error

1

u/Ill-Diet-7719 Jul 11 '25

Apparently there's no error. It's just not print anything. My g.ess would be by continue, I'm somehow skipping everything but not sure how. Code seems fine to me

1

u/maxwellzhang Jul 11 '25

what error did you get

1

u/Ill-Diet-7719 Jul 11 '25

It was not printing anything. Was a problem with ide. Sorted now. Thank you

1

u/maxwellzhang Jul 11 '25

there is no interreter in the IDE?

1

u/maxwellzhang Jul 11 '25

i didnt write this code

1

u/EmuBeautiful1172 Jul 11 '25

That’s the thing with vscode . It has so many buttons and features you might have something selected that your unaware of and it’s making it do that. Try running that same piece of in another IDE

1

u/Ill-Diet-7719 Jul 12 '25

Might you be aware with vscode? There are so many extensions it suggesting and I have no clue

1

u/ninhaomah Jul 11 '25

so after all this yes it runs and no it doesn't run on vs code , there is still no screenshot or anything from OP ?

strange that everyone aand their mothers and grand-mothers post their holiday photos on FB / X and videos on tiktok but won't give a screenshot while asking technical questions.

1

u/Ill-Diet-7719 Jul 12 '25

My bad man. I'll remember to put up ss of the output if ever run into problem again

1

u/Pokepredator Jul 12 '25

I know many people have commented but once you’ve written the code you have to save it like ctrl+s or the save button. VS code is just like a text editor but with extra steps. It runs code from the file so you have to actually save your editions to the file for it to run.

1

u/Ill-Diet-7719 Jul 12 '25

Like should I save and then run? There's a terminal window there and until now I'd just run it there and everything would go good. This was first time something happened and well I kinda panicked ig

2

u/RealLifeRiley Jul 12 '25

Yes, exactly. If you run without saving, it’s running the last saved version of your code, not the work you just did. CTRL s is your friend

2

u/Ill-Diet-7719 Jul 12 '25

So are youđŸ«‚ thanks

1

u/Top-Anteater-8127 Jul 12 '25

sometime you may see indent error based on spaces and tabs l. use either tab or spaces consistent throughout

1

u/Ill-Diet-7719 Jul 12 '25

Before this I used to do html with no respect for spaces. Never went to much into it, school syllabus was very basic, and everything went smooth all the time. It's a bad habit now lmao

1

u/[deleted] Jul 12 '25

OP, you are using VScode and not Visual Studio. They are very different products and some of the answers here think you're using Visual Studio.

1

u/Ill-Diet-7719 Jul 12 '25

Wow I'm a royal dumb ty. Yes I'm using vs code(with the blue logo).

1

u/Relevant_War_4572 Jul 15 '25

you sure that you need two i+=1?

1

u/Ill-Diet-7719 Jul 15 '25

yeah ig..it worked out. so maybe it was or it wasn't lol

2

u/EnzoPei1412 Jul 14 '25

Is it VScode? I will assume this is VScode and you probably didn't set up the autosave, It happend to me when I first started using VScode and it was really frustrating.

Your code is perfectly fine! Good work!

1

u/maxwellzhang Jul 11 '25

works for me

1

u/turtlemaster1993 Jul 11 '25

Is your printer on?

1

u/Hectorreto Jul 11 '25

Maybe vscode was showing the wrong terminal? or executed the file with unsaved changes?

1

u/Agent_Choocho Jul 11 '25

For the pros, would it be more efficient to use an else block instead?

1

u/SergeantSemantics66 Jul 12 '25

When i is even, it hits continue before it can reach print(i) or the second i += So it skips printing the even numbers — which is fine. But you already increment i before the continue, so that’s fine too. But if you didn’t increment i before continue, it would get stuck in an infinite loop.

1

u/rlt0w Jul 12 '25

Is you're using vs code run, don't do that. Instead, open the terminal and execute it with python {file}

1

u/Glittering_Abies_752 Jul 12 '25

Did u install the python environment on the computer? If not pls install it first and then search for python extension in visual code and choose the interpreter only then the code will run

1

u/totalnewb02 Jul 12 '25

have you save it as python file? i had the same problem multiple times, all because i save it in txt.

1

u/Whole_Bid_360 Jul 12 '25

OP I don't think your saving the file.

1

u/TheCarter01 Jul 12 '25

Here's a more simplified version:

for i in range(1, 10, 2): print(i)

1

u/Few_Split1038 Jul 13 '25

No errors. Your program prints all the odd numbers from 1 to 10. The even numbers are skipped.

1

u/Known-Delay7227 Jul 15 '25

Do you have Python installed?

1

u/initumX Jul 16 '25 edited Jul 16 '25

Not a good style (use if-else instead of just if), but it is correct and still should work. It should print 1 3 5 7 and 9. If you don't see, then may be you are using windows and console disappears immediately after starting and you can't see what it shows.

  1. You can add 3 sec delay this way:

2. Or run it from console like this: python yourPyFile

1

u/Owly_chouette Jul 16 '25

don't forget to save your file.

If you want to improve your code you could use list comprehension:

[i for i in range(1,10) if i % 2 == 0]

1

u/RafikNinja Aug 04 '25

Pretty sure the continue is not needed and may cause an issue

1

u/Confident-Orange-628 Sep 20 '25

What output are u expecting my guy?

1

u/Confident-Orange-628 Sep 20 '25

Cause the code ran fine on my pc

0

u/[deleted] Jul 11 '25

[deleted]

2

u/Cerus_Freedom Jul 11 '25
for i in range(2, 11, 2):
  print(i)

1

u/Pyrix25633 Jul 11 '25

Yeah man but what about i+=2, in other scenarios you might want to do something else if it's odd though obviously

1

u/sarc-tastic Jul 11 '25

Nah, use a continue to skip cases you don't care about. Makes it more readable

11

u/[deleted] Jul 11 '25

[removed] — view removed comment

1

u/Ill-Diet-7719 Jul 11 '25

Hahaha lmao but no. I'm doin on vs. they give an option of running right there.

2

u/RealLifeRiley Jul 12 '25

There is still a possibility it’s running the unsaved file. You may still need to save your file first before running. I think ctrl+s does that on vs code, assuming you’re on windows. VS code also displays a dot on the tab of files with unsaved work on them, if I remember correctly.

Totally mighty not be your issue, but I’m just trying to cover all the bases

4

u/Abject-Kitchen3198 Jul 11 '25

As I use Jetbrains IDEs almost exclusively, I might get tripped by this if I use something else.

1

u/[deleted] Jul 12 '25

[removed] — view removed comment

1

u/Ill-Diet-7719 Jul 12 '25

It's sorted now tysm.

1

u/[deleted] Jul 11 '25 edited Jul 11 '25

[removed] — view removed comment

1

u/[deleted] Jul 11 '25

[removed] — view removed comment

2

u/Ill-Diet-7719 Jul 11 '25

Thank you but I'm not getting good hand at loops yet. So trying it out with it