input 只能输入非负整数
时间: 2023-10-09 17:06:29 浏览: 62
As an AI language model, I cannot control or restrict the input provided by a user. However, if you want to only allow non-negative integers as input, you can add a conditional statement in your code to check if the input is a non-negative integer and prompt the user to enter a valid input if it is not. Here is an example in Python:
```
while True:
user_input = input("Please enter a non-negative integer: ")
if user_input.isdigit() and int(user_input) >= 0:
break
else:
print("Invalid input. Please enter a non-negative integer.")
```
This code prompts the user to enter a non-negative integer repeatedly until a valid input is provided. The `isdigit()` method checks if the input contains only digits, and the `int()` function converts the input to an integer. The `>= 0` condition checks if the integer is non-negative. If the input is invalid, the code prints an error message and prompts the user again.
阅读全文