英文:
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('Enter your age: ')
while age:
if int(age) > 0 or int(age) <= 5:
print('you are', age, 'wow')
elif int(age) >= 6 or int(age) >= 10:
print('.')
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) > 0 and int(age) <= 5:
- 以及在这一行中也使用 "<em>and</em>" 而不是 "<em>or</em>",并且仅在年龄大于 6 且小于 10 时运行?
elif int(age) >= 6 and int(age) <= 10:
英文:
You can use a while loop like this:
while True:
age = input('Enter your age: ')
if int(age) > 0 or int(age) <= 5:
print('you are', age, 'very young')
elif int(age) >= 6 or int(age) >= 10:
print('.')
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) > 0 and int(age) <= 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) >= 6 and int(age) <= 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('Enter your age: ')
if int(age) > 0 or int(age) <= 5:
print('you are', age, 'wow')
elif int(age) >= 6 or int(age) >= 10:
print('.')
else:
print()
# Ask user if they want to stop. Only yes or y stops the program. Otherwise it continues
answer = input('Do you want to stop?')
if answer.lower() == 'yes' or answer.lower() == 'y':
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论