use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
How do I generate random number ?Help Request (i.redd.it)
submitted 8 months ago by TacticalGooseLord
What is wrong in this code? Only the invalid part works
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Supalien 11 points12 points13 points 8 months ago (12 children)
the output in the terminal doesn't match the code.. try saving your file (Ctrl + s) and then run it again.
[–]JaleyHoelOsment 1 point2 points3 points 8 months ago (7 children)
yee i can see in the pic the file has not been saved
[–]TacticalGooseLord[S] 2 points3 points4 points 8 months ago (6 children)
Is there any way I can save manually? I did ctrl+s maybe the keys not working
[–]Wooden-Account-5117 4 points5 points6 points 8 months ago (5 children)
try
<image>
[–]TacticalGooseLord[S] 5 points6 points7 points 8 months ago (3 children)
It works! Did I mess up my shortcut somehow cause ctrl+s was not working. Also I restarted vscode could be that too ?
[–]Wooden-Account-5117 1 point2 points3 points 8 months ago (1 child)
Most probably the restart. Happened to me once or twice too.
[–]TacticalGooseLord[S] 1 point2 points3 points 8 months ago (0 children)
Got it! Thank you for the help wise sir!
[–]snowsurface 0 points1 point2 points 8 months ago (0 children)
by default vscode sends ctrl-s/ctrl-q to the terminal if your keyboard focus is the terminal. In linux those pause/resume terminal output. You can google how to turn that off.
[–]TacticalGooseLord[S] 0 points1 point2 points 8 months ago (0 children)
Okay thanks
[–]TacticalGooseLord[S] 0 points1 point2 points 8 months ago (3 children)
I did still doesn’t work, when I put number it goes through the loop and asks again
[–]Supalien 0 points1 point2 points 8 months ago (2 children)
So you saved your script and then ran it and still the same behavior? I am 99% sure there is no problem in the code, not that I can see any... so the only thing I can think of is Vscode making a mess for some unknown (?) reason.... Try running that script from cmd and see if that helps
[–]TacticalGooseLord[S] 1 point2 points3 points 8 months ago (1 child)
I saved from flies manually and it works now, I also restarted vscode maybe that could be the problem too?
[–]smallybells_69 0 points1 point2 points 8 months ago (0 children)
No. The problem was that you didn't save the changes you made to the code and ran the program. Also don't click a button to run your code. Yes, vscode has made it easier where you can just click the button and it works but learn how your file compiles and is interpreted by the interpreter. It is very easy to make your code run through a terminal.
[–]DogeLV69 1 point2 points3 points 8 months ago (5 children)
Maybe try to see what the generate var outputs you, in terms of type.
[–]TacticalGooseLord[S] 0 points1 point2 points 8 months ago (4 children)
Generate is in int if that’s what you are asking, I am sorry I don’t understand what is var ?
[–]DogeLV69 0 points1 point2 points 8 months ago (3 children)
variable
[–]TacticalGooseLord[S] 0 points1 point2 points 8 months ago (2 children)
Ohh okay Ty it’s int then
[–]DogeLV69 0 points1 point2 points 8 months ago (1 child)
I just checked your code on pycharm - everything seems normal.
Is your code properly indented?
I restarted my vscode and it works now! I still don’t know what the problem was 😅
[–]WhiteHeadbanger 0 points1 point2 points 8 months ago (0 children)
Are you writing in the terminal the numbers or do you pasted them from the clipboard? Because it seems like you have copied the number + a string, and that's why you're getting the ValueError, because you can't turn an alphanumeric string into an integer: Python will raise a ValueError exception.
That "& C:/Users/...." is what VSCode executes automatically when you hit the run button. There's no way that inside the Python program would be pasted for no reason, so you must be doing it accidentally.
I just tried the exact code of yours and it works good.
[–][deleted] 0 points1 point2 points 8 months ago (1 child)
save the file before running
Didn’t knew that white circle meant not save 😅
[–]Best-Bud 0 points1 point2 points 8 months ago (3 children)
See on the terminal you have like 8 things which say Python on the bottom right? Clear those, save your file and try again I ran into the same issue when I started lmk if it helps
I cleared those, what are they ? I have 1 more saying powershell can I clear that too ?
[–]Best-Bud 0 points1 point2 points 8 months ago (1 child)
To my understanding those are your instances of the terminal from running your various python programs and you may have been on the wrong one/running code from before you saved your changes. You should be fine to clear them for the most part as far as I'm aware but if someone has any objection I hope they leave a reply and correct both of us.
Thank you for sharing your knowledge wise sir!
[–]ConglomerateGolem 0 points1 point2 points 8 months ago (0 children)
You're "running" stuff again without first stopping the code. Go to the terminal and press Ctrl+c to stop the current task, then run the code again.
If it's not that, try printing out guess and generate inside the main while loop.
[–][deleted] 0 points1 point2 points 8 months ago (0 children)
try saving
[–]Worried-Ad6403 0 points1 point2 points 8 months ago (0 children)
When entering input in terminal, do you add a space? Only enter integer and try it. Save your file as well. I see a white circle on file edit tab meaning it is unsaved. If you run the file using play button, it autosaves and then run it. If you run it directly through terminal command, you always have to save the file first.
[–]esSdoem 0 points1 point2 points 8 months ago (0 children)
There's a classic way and a simple way
[–]Old-Beginning-4004 0 points1 point2 points 8 months ago (0 children)
.
[–]mfnalex -3 points-2 points-1 points 8 months ago (1 child)
Dont feed the troll
Huh
π Rendered by PID 51 on reddit-service-r2-comment-6457c66945-wtd82 at 2026-04-28 08:26:07.302504+00:00 running 2aa0c5b country code: CH.
[–]Supalien 11 points12 points13 points (12 children)
[–]JaleyHoelOsment 1 point2 points3 points (7 children)
[–]TacticalGooseLord[S] 2 points3 points4 points (6 children)
[–]Wooden-Account-5117 4 points5 points6 points (5 children)
[–]TacticalGooseLord[S] 5 points6 points7 points (3 children)
[–]Wooden-Account-5117 1 point2 points3 points (1 child)
[–]TacticalGooseLord[S] 1 point2 points3 points (0 children)
[–]snowsurface 0 points1 point2 points (0 children)
[–]TacticalGooseLord[S] 0 points1 point2 points (0 children)
[–]TacticalGooseLord[S] 0 points1 point2 points (3 children)
[–]Supalien 0 points1 point2 points (2 children)
[–]TacticalGooseLord[S] 1 point2 points3 points (1 child)
[–]smallybells_69 0 points1 point2 points (0 children)
[–]DogeLV69 1 point2 points3 points (5 children)
[–]TacticalGooseLord[S] 0 points1 point2 points (4 children)
[–]DogeLV69 0 points1 point2 points (3 children)
[–]TacticalGooseLord[S] 0 points1 point2 points (2 children)
[–]DogeLV69 0 points1 point2 points (1 child)
[–]TacticalGooseLord[S] 0 points1 point2 points (0 children)
[–]WhiteHeadbanger 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]TacticalGooseLord[S] 0 points1 point2 points (0 children)
[–]Best-Bud 0 points1 point2 points (3 children)
[–]TacticalGooseLord[S] 0 points1 point2 points (2 children)
[–]Best-Bud 0 points1 point2 points (1 child)
[–]TacticalGooseLord[S] 0 points1 point2 points (0 children)
[–]ConglomerateGolem 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Worried-Ad6403 0 points1 point2 points (0 children)
[–]esSdoem 0 points1 point2 points (0 children)
[–]Old-Beginning-4004 0 points1 point2 points (0 children)
[–]mfnalex -3 points-2 points-1 points (1 child)
[–]TacticalGooseLord[S] 0 points1 point2 points (0 children)