GASP/GASP_Code
6
1
Fork
You've already forked GASP_Code
4

read_yesorno is broken #13

Closed
opened 2021年04月12日 03:01:23 +02:00 by RichardMartinez · 1 comment

In gasp.utils, the read_yesorno() function appears to be broken. When you call the function:

>>> from gasp.utils import read_yesorno
>>> read_yesorno()
Yes or no? no
Please answer yes or no.
Yes or no? yes
Please answer yes or no.

I looked at the code and I think I found the issue. On line 26 line of utils.py, the code seems to always throw an error:

AttributeError: module 'string' has no attribute 'lower'

I think this is because "string.lower()" is being parsed as referencing the string module that is imported in line 1.

Since there is no "lower" or "split" in dir(string), line 26 will always throw an AttributeError. Because of this, the try except block will always go down to except and set result = "". This then messes with the rest of the function because neither of the if statements will be triggered. Since nothing is returned, the function enters an infinite loop.

I don't know exactly the purpose of line 26, so I made a few small changes to the function and it works perfectly:

def read_yesorno(prompt="Yes or no? "):
 if prompt != "" and prompt[-1] not in string.whitespace:
 prompt += " "
 while True:
 result = input(prompt)
 if result.lower() in ["y", "yes"]:
 return True
 elif result.lower() in ["n", "no"]:
 return False
 print("Please answer yes or no.\n")

Hopefully this helps and could be implemented in some way.

In gasp.utils, the read_yesorno() function appears to be broken. When you call the function: ``` >>> from gasp.utils import read_yesorno >>> read_yesorno() Yes or no? no Please answer yes or no. Yes or no? yes Please answer yes or no. ``` I looked at the code and I think I found the issue. On [line 26](https://codeberg.org/GASP/GASP_Tkinter/src/branch/main/gasp/utils.py#L26) line of utils.py, the code seems to always throw an error: `AttributeError: module 'string' has no attribute 'lower'` I think this is because "string.lower()" is being parsed as referencing the string module that is imported in [line 1](https://codeberg.org/GASP/GASP_Tkinter/src/branch/main/gasp/utils.py#L1). Since there is no "lower" or "split" in dir(string), line 26 will always throw an AttributeError. Because of this, the try except block will always go down to except and set result = "". This then messes with the rest of the function because neither of the if statements will be triggered. Since nothing is returned, the function enters an infinite loop. I don't know exactly the purpose of line 26, so I made a few small changes to the function and it works perfectly: ``` def read_yesorno(prompt="Yes or no? "): if prompt != "" and prompt[-1] not in string.whitespace: prompt += " " while True: result = input(prompt) if result.lower() in ["y", "yes"]: return True elif result.lower() in ["n", "no"]: return False print("Please answer yes or no.\n") ``` Hopefully this helps and could be implemented in some way.

Great catch, Richard! The string module had all these functions back in Python 2, but they were removed, and changed to methods of the string object, in Python 3.

I thought I had made all the changes needed, but I missed that one.

Great catch, Richard! The string module had all these functions back in Python 2, but they were removed, and changed to methods of the string object, in Python 3. I thought I had made all the changes needed, but I missed that one.
Sign in to join this conversation.
No Branch/Tag specified
main
No results found.
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
GASP/GASP_Code#13
Reference in a new issue
GASP/GASP_Code
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?