Python Hexadecimal errors when hitting back-space while using inquirer module

Click For Summary
The discussion revolves around issues encountered while using the 'Text' feature from the 'inquirer' module in a coding project. The primary problem arises when users input data and utilize the backspace key, resulting in errors due to the presence of backspace characters (\x08) in the input string. This leads to a ValueError when attempting to convert the string to an integer. Additionally, an error occurs when backspacing after providing invalid input, causing the program to display the wrong error message.The consensus is that the 'inquirer' module does not adequately handle backspace characters, which complicates input validation. To resolve this, it is suggested to implement custom code to process the input string and remove backspaces. A reference to a resource on handling backspace characters in strings is provided for further assistance.
Eclair_de_XII
Messages
1,082
Reaction score
91
TL;DR
Basically, the inquirer module is like a module for advanced forms of user-input, like having the user select from a list, multiple choice, text, etc. Documentation is here.

https://python-inquirer.readthedocs.io/en/latest/
I'm using this user-defined module for an amateur coding project, and I am using 'Text' from the 'inquirer' module. When I am either inputting things, or whenever I am validating things, I always get some sort of error. These errors mainly result whenever I hit the back-space key on my keyyboard. I am reconsidering doing this using the 'raw_input' built-in function to do so, but it would be nice to know how to figure out the source of my problem.

This is a sample of code that I wrote that illustrates one of the two dilemmas I am having.

[CODE lang="python" title="Sample code that produces erroneous input"]from inquirer import errors, prompt, Text

def check_even(ans,cur):
if int(cur)%2!=0:
raise errors.ValidationError('',reason='The inputted integer is not even!')
return True

def is_this_even(hex10_error=True):
if hex10_error:
val=True
else:
val=check_even
question=[Text(
name='',
message='Please input some integer...',
validate=val
)]
return int(prompt(question)['']) % 2 == 0

if __name__=='__main__':
error=True
even=is_this_even(error)
print(even)[/CODE]

This error passes the error (back-space and all) and then gets an error when the function attempts to convert the string number to integer. My main objective here is to get rid of all these \x08 from the backspaces.

[CODE title="Error 1: Hexadecimal str recorded, not literal string" highlight="7"][?] Please input some integer...: 4564
Traceback (most recent call last):
File "C:/Users/Eclair/Documents/3 Projects/Project Aenir/test.py", line 17, in <module>
even=is_this_even()
File "C:/Users/Eclair/Documents/3 Projects/Project Aenir/test.py", line 14, in is_this_even
return int(prompt(question)['']) % 2 == 0
ValueError: invalid literal for int() with base 10: '4564\x08\x08\x08\x08\x08'[/CODE]

The second error results when I back-space after initially giving bad input. After giving bad input and back-spacing, the program gives the error message in line 12, instead of the expected one in line 13.

[CODE title="Error 2: Back-spacing after giving bad input." highlight="12,13"][?] Please input some integer...: 4545

>> "454" is not a valid .
>> The inputted integer is not even![/CODE]

I honestly do not know enough about coding to know why this happens, so please forgive the lack of effort on my part.
 
Technology news on Phys.org
The problem appears to be that the user-defined module you are using doesn't handle backspacing. Basically it returning a string to you with the bad digits and the backspacing to remove them.

You will need to write some code to walk the string and handle the backspacing unless there is an option with the user-defined Inquirer module to do that. It doesn't seem so as I looked at it docs briefly online.

The code below handles removing the backspaces but you'll have to replace the '#' with the backspace character.

https://www.geeksforgeeks.org/string-after-processing-backspace-characters/
 
  • Like
Likes Eclair_de_XII and Ibix
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K