216k views
5 votes
Write a Python script named counting-up.py which reads a positive integer n from standard input and outputs all of the numbers from 0 to n−1 (inclusive), one per line. Example standard input and standard output Counting up again. Write a Python script named counting-up-2.py which reads a positive integer n from standard input and outputs all of the numbers from 1 to n (inclusive), one per line.

User NightOwl
by
10.3k points

1 Answer

4 votes

Final answer:

To solve this problem, you can use a for loop to iterate from 0 to n-1 (inclusive) and print each number on a separate line using the print() function. For the second part of the question, modify the script to iterate from 1 to n (inclusive) by changing the range in the for loop.

Step-by-step explanation:

To solve this problem, you can use a for loop to iterate from 0 to n-1 (inclusive). Inside the loop, you can print each number on a separate line using the print() function. Here's an example of how you can write the counting-up.py script:

n = int(input('Enter a positive integer: '))

for i in range(n):
print(i)

This script reads the input integer n using the int(input()) function and then uses a for loop to iterate from 0 to n-1. For each iteration, it prints the current number i on a separate line.

For the second part of the question, you can modify the script to iterate from 1 to n (inclusive) by changing the range in the for loop. Here's an example:

n = int(input('Enter a positive integer: '))

for i in range(1, n+1):
print(i)

In this modified script, the range starts from 1 instead of 0, and the end value is n+1 to include the number n in the output.

User Esraa Abdelmaksoud
by
8.1k points