我是Python的新手,学习了For Loops并编写了以下代码:
cars = [1, 2, 3, 4]
trucks = ['red', 'blue', 'green', 'white']
for numbers in cars:
print (f"I can count: {numbers}")
for colors in trucks:
print(f"I can list colors: {colors}")
new_list = []
for numbers in range(0, 4):
new_list.append(numbers)
for numbers in new_list:
print(f"I can count more: {new_list}!")
我得到这个结果:
I can count: 1
I can count: 2
I can count: 3
I can count: 4
I can list colors: red
I can list colors: blue
I can list colors: green
I can list colors: white
I can count more: [0, 1, 2, 3]!
I can count more: [0, 1, 2, 3]!
I can count more: [0, 1, 2, 3]!
I can count more: [0, 1, 2, 3]!
对于“我可以计数更多”部分,如何获取它以数字顺序列出范围:
I can count more: 1!
I can count more: 2!, etc.
而不是打印行中的整个范围?
答案 0 :(得分:5)
# Login using OAuth2.
@csrf_protect
def login(request):
next_page = request.path
if next_page is None or next_page == '':
next_page = request.POST.get('next', request.GET.get('next', ''))
# Check if they are already logged in and redirect them to the original page if so.
if hasattr(request, 'user') and request.user.is_authenticated:
return HttpResponseRedirect(next_page)
# Otherwise, send them to the OAuth2 page with the request url as the next parameter.
else:
return HttpResponseRedirect('/soc/login/google-oauth2?next=' + next_page + '&hd=mydomain.com')
应该是
print(f"I can count more: {new_list}!")
因为print(f"I can count more: {numbers}!")
是整个列表,而new_list
是您在numbers
中定义的for
变量
答案 1 :(得分:0)
使用“数字”代替“ new_list”。并且不要在同一列表上一次又一次地迭代,因为new_list包含您在上一个循环中显示的相同项目。所以,您的代码应该是这样的,
for numbers in range(0, 4):
print(f"I can count more: {numbers}!")