Home >Backend Development >Python Tutorial >How to Resolve Keyboard Input Issues in Python?

How to Resolve Keyboard Input Issues in Python?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-22 11:19:291014browse

How to Resolve Keyboard Input Issues in Python?

Keyboard Input in Python

You've encountered an issue reading keyboard input in Python using the code:

<code class="python">nb = input('Choose a number')
print('Number%s \n' % (nb))</code>

This code doesn't function because:

  • In Python 3, input prompts the user for input without printing anything. Use input('Enter your input:') instead.
  • If you want a numeric value, convert the input using try/except:
<code class="python">try:
    mode = int(input('Input:'))
except ValueError:
    print("Not a number")</code>
  • In Python 2, use raw_input instead of input.

The above is the detailed content of How to Resolve Keyboard Input Issues in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn