简体   繁体   中英

Python How to Change Value in Loop

There's a code I'm attempting to reverse-engineer for an assignment. The program asks you to enter an integer and it'll loop the words "Line (loop it's on) Hello World" as many times as the number you entered. On the first loop, it said 'Line 1', on the second, it said 'Line 2' and on the third, it said 'Line 3' etc all the while repeating the Hello World right after.

How would I achieve the effect of changing the number each loop? Also, how do I make it loop as many times as the number inputted? My version is Python 3.4.

Something like this?

for n in range(int(input())):
    print("Line", n+1, "Hello World")

The range function gives you an iterator with a value 0 , 1 , 2 , ..., n-1 .

If this is what you want?

>>> while 1:
...     for n in range(int(input())):
...         print("Hello World")
... 

The output like this:

1
Hello World
2
Hello World
Hello World
4
Hello World
Hello World
Hello World
Hello World

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM