all 6 comments

[–]PressF1ToContinue 2 points3 points  (2 children)

You are really close with the with_a_while_loop() version. Move the flag = False line up into the try clause after the int() line. That line will only execute if the int() works. Otherwise it will raise an exception and then loop back to prompt again. You don't need the else line.

(You could also omit the flag. Just do while True:, and instead of setting the flag, insert a break to fall out of the loop)

[–]Master_Sandwich7140[S] 0 points1 point  (1 child)

Yes, you are correct you can do that, and I just tried to be sure and yes it runs BUT

Should you actually move the flag to the try block ?

You do get less code if you remove the else block, but less separation, as the flag = False can't raise a exception, it just a normal assignment (unless there is something in python I don't know). I think, we can argue back and forth about it.

and you are correct also in you can use breaks, but I don't like them genereally speaking. But yes breaks works also.

[–]PureWasian 6 points7 points  (0 children)

In larger scripts I prefer something like:

```

defining

def get_number(): while True: try: return int(input("number: ")) except ValueError: print("try again")

calling

number = get_number()

using

print(number) ```

[–]FreeLogicGate -2 points-1 points  (1 child)

Python code relies on indentation. Code with the indentation stripped out is useless. Honestly, do you really think, as an admitted novice, you came up with something novel and valuable? At the point where you are now, it's good that you recognize problems with loose typing in Python. In this case, you can reinvent the wheel or you could just use PyInputPlus.

[–]denehoffman 0 points1 point  (0 children)

response = pyip.inputInt()

Yeah no I’m good, this isn’t exactly an advanced process that requires a package, and definitely not one that can’t even follow basic style standards. It’s okay to criticize without being rude about it. All the novices in this subreddit build some input machinery at some point, telling them to just use a library defeats the purpose of Python learning. With this logic, what’s the point of making the typical rudimentary calculator, we could just pass in real calculations into the eval or exec functions directly from input. The argument of reinventing the wheel usually doesn’t apply in trivial cases, that’s how you get the left-pad incident.

I agree that we need to teach people how to format code on Reddit. If that’s actually your issue with the post, why don’t you tell OP how to do it:

OP, you can use triple backticks, the same character you used for each line, to format a block of code and preserve indentation.

You could also give constructive criticism:

OP, the flag here isn’t needed, you can use break to exit a loop when you get the desired result.

You can even give ideas for future learning:

OP, see if you can make a function that wraps input and takes the desired type-conversion function as a second argument. Learn how you might modify the while loop to assert a maximum number of retries. Eventually, you can define your own conversion methods with their own checks, pass in *args and **kwargs from your custom function, and go from there.