while True:
timestoprint = input("How many times to print hello?")
if timestoprint.isnumeric(): # Check if input is a number
timestoprint = int(timestoprint) # Convert it to number
break # Break the while loop
else: # if it is not a number
print("The input is not a number.")
首先,您需要将输入转换为整数:
然后您必须使用
range
内置生成器并使用它射程是如何工作的
您可以选择在以下任何配置中为range提供参数:
range(number)
它以1的增量生成从0到数字1的计数range(start, stop)
以1的增量生成从开始到停止的计数-1range(start, stop, step)
在步进增量处生成从开始到停止的计数-1您可能还希望验证用户输入,这可以通过使用永久while循环替换input语句来完成,该循环在用户提供有效输入后中断
你写的
而不是
您还忘了将
timestopprint
转换为int
这对我很有用:
首先,
input
返回一个字符串,而您需要一个int
。你需要做一个转换。其次,for循环只接受iterables,而不是整数,因此您希望使用range
来获取要迭代的值相关问题 更多 >
编程相关推荐