How to Resolve Keyboard Input Issues in Python?

Patricia Arquette
Release: 2024-10-22 11:19:29
Original
904 people have browsed it

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>
Copy after login

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>
Copy after login
  • 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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!