r/PythonLearning • u/SkizzyBeanZ • Jan 15 '25
Beginner not sure why input is skipped
Im trying to undertake an excersize on freecodecamp which wants my to form a loop to repeat everytime a numerical value has been inputted and end when the user types done.
I have no idea of my code is going to work nor can I test it because it blows up at line 10 because theres no input. However the code seems to skip the input as I am not prompted to input something.
What seems to be the problem? I cant wrap my head around it. Ive used input many times and haven’t ran into this issue before.
3
u/watakushi Jan 15 '25
There are a few problems with your code, most of which a properly configured IDE would display for you even before running the code.
As others have mentioned:
- If you want to compare 2 numbers, they must both be int or float, your input gets a string.
- true can be the name of a variable, if you want a boolean, it must be spelled True
- If you ask for input outside the while statement, you'll only ever gonna get one psv value, and after the first comparison, value will get the psv value, and through the while loop, it will keep checking it against the value, which is the same, so the = part of the comparison will always be True, hence, an infinite loop.
- else doesn't get a condition, it's just else: .
- A better approach would be to instead of doing while True: do while psv != 'done': and convert the psv to int inside the while loop.
All that being, I still don't understand what your program is supposed to do, you ask for a 'psv' then compare it to a number that will always become the last psv you entered and then you print that value, regardless of which if statement you go into...
It's printing the input value with extra steps. Unnecessarily convoluted imo.
1
u/SkizzyBeanZ Jan 15 '25
Thankyou for the info. The task is to have a user input numbers over and over again until the user inputs done which will then end the program printing “Done”
1
u/SkizzyBeanZ Jan 15 '25
Since I cant edit the post. For those who commented. Thank you for your help. Managed to get it sorted and completed the task.
While value != … helped make the program less complicated! Appreciate it all.
1
u/Necessary-Bit3089 Jan 15 '25
Wouldn't it be easier like:
value = 0
while value != "Done":
value = input("Gimme number or Done")
print("Done!")? Ofc you cant do any math with the numbers, but if he would ever need to use numbers for math or he would need them as int, he can do some code for check if its number and if yes, store it in lets say array but thats way more advanced then just doing this little check for "Done"
1
u/SkizzyBeanZ Jan 15 '25
Oh wow that is a lot more simple.
1
u/Necessary-Bit3089 Jan 15 '25
If you also need to store the numbers you can go with something like this in simple way:
value = 0 while value != "Done": value = input("Add number") if value.isdigit(): number = int(value) print ("Your input was indeed int and it was:", number) print("Done")But hence that it does not support negative numbers or numbers with space character before them etc.
You could also do a little code to make array for the numbers so it won't just overwrite the last number everytime but I guess that would be more complex to you since you are now doing excersice about while loops :)
EDIT: Added print with the number
1
u/FoolsSeldom Jan 20 '25 edited Jan 20 '25
How about,
value = 0 while (psv := input("Gimme number or Done")) != "Done": ...
2
u/No-Remote7748 Jan 16 '25

what it should look like.
The user input (psv) is taken outside the while loop, so it won’t update with each user input inside the loop.
Since
psvis a string (frominput()) andvalueis an integer, directly comparing them will raise a TypeError. You need to convertpsvto an integer first.The
elifconditionpsv >= valueis redundant because if the firstifconditionpsv <= valueis false, theelifwill always be true.The
elseclause is incorrectly formed withelse psv == "done"; it should beelif psv == "done".
1
u/jaynabonne Jan 15 '25
Not sure what you mean by "blows up", but looking directly at line 10, it's almost certainly not what you meant and at least semantically, if not syntactically, invalid.
1
1
1
Jan 16 '25
The value is a str not an int, and the way you are using it will raise a type error. Also It seems when defining value you haven't wrapped the value of value with "" or ' '
1
u/realxeltos Jan 16 '25
First and second command bothe have one of the same conditions (==).
Else statement does not need a condition. Else statement is wrong. psv == 'done' is checking if psv value is the string 'done' which is weird.
If you want to store 'done' as psv then you only use one =
1
Jan 16 '25
Follow the advice the other people gave you and also ditch Atom. It is discontinued. Any proper IDE would highlight most of the issues you had before running the code.
25
u/FoolsSeldom Jan 15 '25
Truerather thantrueinputreturns astrobject, not a number object, and you can't do maths (or math comparisons) on a stringuse
intto conver the strings to integerscheck for done before converting
you are missing another
inputinside of your loop, so will never break if first entry isnt done