简体   繁体   English

如何重复“加载”字符串

[英]How do i make the string of “loading” repeat

So I'm working on a project at school and I'm wondering how to make certain strings repeat. 所以我正在学校里做一个项目,我想知道如何重复某些字符串。 As an example, I made a small piece of code that has a string to that could be repeated. 作为示例,我编写了一小段代码,其中包含可以重复的字符串。 How do I repeat the string "loading..."? 如何重复字符串“正在加载...”? (Obviously, half the code is missing as this is just an example!) (显然,缺少一半的代码,因为这只是一个例子!)

import sys
import time
print("Welcome to the loading screen of every game!!!")
yesNo = input("Do you want to continue(Y or N): ").upper()
if yesNo == "Y":
    print("Loading...")
    time.sleep(10)

sys.exit()

Use a loop. 使用循环。

for i in range(5):
    print("Loading...")
    time.sleep(10)

Can you define what you mean by "repeat"? 您能否定义“重复”的含义? If I understand the meaning of your code snippet correctly, you want "Loading..." to keep printing as long as the user is entering a "Y"? 如果我正确理解了代码段的含义,那么只要用户输入“ Y”,是否希望“ Loading ...”继续打印?

This is a great use for a while loop ! 这对于while循环很有用! I would recommend putting it into an infinite loop ( while True ) and then break out when a certain condition has been satisfied. 我建议将其放入无限循环( while True ),然后在满足特定条件时break

You could either use a while statement or for statement. 您可以使用while语句或for语句。

Also I would assume you would only want it to do this for a certain amount of time and not infinitely in which case range() might also be useful. 我也假设您只希望它在一定时间range()执行此操作,而不是无限地执行此操作,在这种情况下, range()也可能有用。

Method 1: using for : 方法1: for

for i in range(5):
    print("Loading...")
    time.sleep(10)

This output would be: 输出为:

Loading...
Loading...
Loading...
Loading...
Loading...

Method 2: using while : 方法2:使用while

 i = 5
 while i < 5:
     print("Loading...")
     time.sleep(10)
     i = i + 1

This output will be: 该输出将是:

Loading...
Loading...
Loading...
Loading...

Method 3: infinite looping: 方法3:无限循环:

 while True:
     print("Loading...")
     time.sleep(10)

This will keep printing "Loading..." forever. 这将永远打印“ Loading ...”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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