python - 如何从 for 循环中的数字 1 开始?
问题描述
我希望第一个输出是“输入进程 1 的突发时间”而不是“进程 0”。我该怎么做?
num = int(input('Enter the number of processes: '))
for i in range(num):
b = input('Enter the burst time of process ' + str(i) + ': ')
a = input('Enter the arrival time of process ' + str(i) + ': ')
解决方案
如果没有起始参数,Python 的 range 函数会返回从 0 到给定数字之间的整数。例如:
for i in range(3):
print (i)
返回:
0
1
2
如果您想更改代码以打印从 1 开始并包含给定输入的范围,您可以考虑将函数稍微更改为:
num = int(input('Enter the number of processes: '))
for i in range(1,num+1):
b = input('Enter the burst time of process ' + str(i) + ': ')
a = input('Enter the arrival time of process ' + str(i) + ': ')
如果您不希望您的范围包含给定的整数,您可以这样做:
num = int(input('Enter the number of processes: '))
for i in range(1,num):
b = input('Enter the burst time of process ' + str(i) + ': ')
a = input('Enter the arrival time of process ' + str(i) + ': ')
推荐阅读
- bounds - 设置 NSTextAttachment 边界在 iOS 15 上不起作用
- r - 通过识别函数的瓶颈来改进速度函数
- flutter - TextField 和 BLoC - 值未更新或键盘已关闭
- python - comparing more than two lists
- java - 我的远程 RMI 呼叫缺少什么?
- flutter - 内部列表视图达到顶部时如何最小化 DraggableScrollableSheet?
- c++ - 在 c 中调用 c++ 函数会导致致命错误
- java - 如何测试存储库类,其中包含另一个已单独测试的存储库类?
- c# - 如何为 IXmlSerializable WriteXml 省略或配置包装器元素
- java - 如何在Java的链表中迭代地添加新节点?