python-3.x - 如何获得正确的号码?
问题描述
--Python 这段代码有什么问题?
a=int(input('enter a number'))
b=list(range(1,11))
if a not in b:
int(input('enter a number'))
else :
print('ok')
输出:
enter a number 89
enter a number 8
解决方案
请进一步解释您正在尝试做什么,如果您尝试为“8”的第二个输入输出“OK”,那么您需要更改此代码的逻辑。
a=int(input('输入一个数字'))
这一行打印“输入一个数字”,然后你的 if 语句检查它是否在 b 内,但如果不是,它再次打印“输入一个数字”而不用它做任何事情,如下一行所示:
int(input('输入一个数字'))
如果您希望代码多次运行 if-else 块,请尝试使用 for 循环或 while 循环。
- 另外,请注意您没有再次将输入放入变量 a 中。
编辑:试试这个代码:
print('enter a number')
a=int(input())
while a not in range(1,11):
print('enter a number')
a=int(input())
else :
print('ok')
推荐阅读
- gatsby - Gatsby:TypeError:无法读取未定义的属性“页面”
- android - Kotlin Compose,在行中对齐项目
- django - Django 发送批量电子邮件
- c# - 如果在例程中多次访问对象的属性,创建变量是最佳做法吗?
- python-requests - Python requests.get 仅针对 Google Cloud Function 中的一个特定 url 返回 500,在本地机器上运行良好
- actions-on-google - Google Action 以“抱歉,出了点问题。请稍后重试”的形式发送响应。在交易流程中
- mysql - 我应该在 sql 中使用 FOREIGN KEY 还是 ADD INDEX?
- oracle - 是否可以将存储过程名称作为另一个存储过程的输入参数并执行它?
- c - 如何在不崩溃的情况下更改程序以执行相同的任务?
- node.js - mongoose 查询中的多个人口,如果一个失败,有没有办法获得其余的填充数据