“Examples of While loops”

huangapple go评论117阅读模式
英文:

Examples of While loops

问题

以下是您要翻译的代码部分:

我是Python新手正在尝试学习while循环我已经编写了一个程序它会要求用户输入他们的年龄然后在年龄输入后打印一句话然后程序结束但我希望它能够在Shell中重新开始提问因为我厌倦了每次都要按F5键

我尝试在循环的末尾添加'return'但它不起作用

age = input('输入您的年龄:')

while age:

    if int(age) > 0 or int(age) <= 5:
        print('你', age, '岁,哇!')
    elif int(age) >= 6 or int(age) >= 10:
        print('.')
    else:
        print()
    return

这就是我所做的
英文:

I am new to python and i am trying to learn while loops, i have written a program where it asks the user for their age, and after the age is entered, a sentence is printed and the program ends, but i want it to restart the question in the shell because i am sick of pressing f5 each time.

I have tried to put 'return' at the end of the loop but it does not work.

age = input(&#39;Enter your age: &#39;)

while age:

    if int(age) &gt; 0 or int(age) &lt;= 5:
        print(&#39;you are&#39;, age, &#39;wow&#39;)
    elif int(age) &gt;= 6 or int(age) &gt;= 10:
        print(&#39;.&#39;)
    else:
        print()
    return

this is what i have done.

答案1

得分: 1

以下是您要翻译的内容:

  • 我创建了一个无限循环的 while True 循环。这个循环无休止地重新运行以下代码,因为在这种情况下,"True" 是条件。该循环重复执行以下代码,直到条件为 False,但它不能为假,因为它硬编码为 "True"。
  • 另外,我将输入语句放入了 while 循环中,因为 input() 语句会等待用户提交文本。

希望能对您有所帮助。

编辑部分:

  • 你是否可能希望在这一行中使用 "<em>and</em>" 而不是 "<em>or</em>"?
if int(age) &gt; 0 and int(age) &lt;= 5:
  • 以及在这一行中也使用 "<em>and</em>" 而不是 "<em>or</em>",并且仅在年龄大于 6 且小于 10 时运行?
elif int(age) &gt;= 6 and int(age) &lt;= 10:
英文:

You can use a while loop like this:

while True:
    age = input(&#39;Enter your age: &#39;)
    if int(age) &gt; 0 or int(age) &lt;= 5:
        print(&#39;you are&#39;, age, &#39;very young&#39;)
    elif int(age) &gt;= 6 or int(age) &gt;= 10:
        print(&#39;.&#39;)
    else:
        print()

changes:

  • I created a while True loop. This loop reruns the following code endlessly, because in this case the "True" is the condition. The loop executes the following code repetitively, until the condition is False, but it cant be false, because its hardcoded to "True".
  • also I have put the input statement into the while loop, because the input() statement waits until the user submitted text.

I hope I could help you

Edit:
is it possible that you wanted to use <em>and</em> instead of <em>or</em> in this line?

if int(age) &gt; 0 and int(age) &lt;= 5:

and in this line also <em>and</em> instead of <em>or</em> and that it only runs if age is bigger than 6 and smaller than 10?

elif int(age) &gt;= 6 and int(age) &lt;= 10:

答案2

得分: 1

你的方法几乎正确,只是 return 语句部分有误。你可以使用一个布尔变量来控制是否要继续循环。另外,你可以询问用户是否要继续循环,根据他们的答案来决定是停止循环还是继续。以下是一个示例:

loop = True
while loop:
    age = input('输入你的年龄:')
    if int(age) > 0 or int(age) <= 5:
        print('你', age, '岁,哇!')
    elif int(age) >= 6 or int(age) >= 10:
        print('.')
    else:
        print()
    
    # 询问用户是否要停止。只有回答 "yes" 或 "y" 才会停止程序,否则继续。
    answer = input('你想停止吗?')
    if answer.lower() == 'yes' or answer.lower() == 'y':
        loop = False

**注意**:`return` 语句用于从函数中返回变量或值它不会返回到循环中你可以在[这里](https://realpython.com/python-return-statement/#:~:text=The%20Python%20return%20statement%20is%20a%20special%20statement%20that%20you,can%20be%20any%20Python%20object.)阅读更多有关 `return` 语句的信息。

<details>
<summary>英文:</summary>

You were pretty close to your approach except for the `return` statement. You can use a `Boolean` variable to control if you want your loop to continue looping. Additionally, you can ask the user if they want to continue looping and depending on their answer, the loop stops or continues. Here is an example:

    loop = True
    while loop:
        age = input(&#39;Enter your age: &#39;)
        if int(age) &gt; 0 or int(age) &lt;= 5:
            print(&#39;you are&#39;, age, &#39;wow&#39;)
        elif int(age) &gt;= 6 or int(age) &gt;= 10:
            print(&#39;.&#39;)
        else:
            print()
    
        # Ask user if they want to stop. Only yes or y stops the program. Otherwise it continues
        answer = input(&#39;Do you want to stop?&#39;)
        if answer.lower() == &#39;yes&#39; or answer.lower() == &#39;y&#39;:
            loop = False

**Note**: `return` is used when you want to return a variable/value from a function. It does not `return` to the loop. You can read more about the `return` statement [`here`](https://realpython.com/python-return-statement/#:~:text=The%20Python%20return%20statement%20is%20a%20special%20statement%20that%20you,can%20be%20any%20Python%20object.).

</details>



huangapple
  • 本文由 发表于 2023年4月6日 23:59:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定