首页 > 解决方案 > 如何从 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) + ': ')

标签: pythonstringoutput

解决方案


如果没有起始参数,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) + ': ')

推荐阅读