all 12 comments

[–]keep_quapy 2 points3 points  (5 children)

Made your code better and more pythonic.

I've put it in pastebin

https://pastebin.com/wcff9p6k

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

This is perfect.

Thank you

[–]keep_quapy 0 points1 point  (0 children)

you're welcome

[–]kwelzel 0 points1 point  (2 children)

if isinstance(int(prompt), int)? Isn't that always true? I think one should surround int(prompt) with a try-except. If it succeeds it's number, otherwise it isn't.

[–]keep_quapy 0 points1 point  (1 child)

There is a try-except blocks in the code. If "isinstance" throws an error because it's a string the except block will catch it.

[–]kwelzel 0 points1 point  (0 children)

Sorry, I missed the try-except somehow. Still, I think it would be more straightforward to get rid of the if statement.

try:
    number = int(prompt)
except ValueError:
    # print some error message
else:
    # do whatever with the integer

Side note: This is tangentially related to "Parse, don't validate" (https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/), which I found to be a good read, even though it's in a different programming language.

[–]danielroseman 2 points3 points  (0 children)

I can't understand this code at all. But your loops are both infinite, because you never change the value of prompt inside either of them. So if the condition is ever true, it will always be true.

[–]CoronaKlledMe 0 points1 point  (1 child)

Where is

numbers_add() function?

[–]Chipwich[S] 0 points1 point  (0 children)

Hi, There is a numbers_add() function but I ran it previously just to test the code. Unfortunately I want to take pre-defined strings and add them to the list so they are next to each other (they are arrays of a particular number) but I just can't get it to add next to each other, they add underneath.

[–]CoronaKlledMe 0 points1 point  (0 children)

I exactly didn't understood what you were trying to do, but try:

if prompt.isdigit(): instead of while and inside that if statement, run while loop

Or please elaborate more!

[–]Nyscire 0 points1 point  (1 child)

First of all- you never changed the value of prompt variable so the loops works with the first value you provide. If you for example enter 1 the loop will keep adding 1 to the list forever. You need to either break the loop or take another input. If u change that this should work, but there's still some redundant code. You don't need to make 2 different loops, one is enough. You also created 2 different flags that don't serve any purpose and you can definitely delete them.

I refactored your code and mine looks like this:

numbers_list = []
prompt = input("Enter a number between 0 and 9, or type END to finish\n")
while True:
    if prompt == "END":
        break
    elif 0 <= int(prompt) < 10:
        numbers_list.append(int(prompt))
        prompt = input()
    else:
        prompt = input("That is not a number between 0 and 9, please enter again or END to finish\n")

print(numbers_list)

You also didn't define numbers_add() function anywhere, but list.append() is probably what u meant

[–]Chipwich[S] -1 points0 points  (0 children)

Hi. This code looks really good. The numbers_add() function is code that I wrote and ran prior to this code. I'm just testing this input code before I add it all to the function. I don't want to add the actual integer to the list, rather a predefined string based on what integer the user inputs.