gpt4 book ai didi

python - 我将工作代码复制并粘贴到我的 IDE 中——现在 Python 抛出了大量错误

转载 作者:太空宇宙 更新时间:2023-11-04 10:37:20 25 4
gpt4 key购买 nike

我将代码复制并粘贴到我的 IDE (TextWrangler) 中。现在,当我尝试运行我的代码时,我收到大量关于缩进和无效语法的随机错误。

在我将代码从一个 Django View 复制并粘贴到另一个之前,代码运行良好。我几乎 100% 确定代码在我的新 View 中仍然是正确的,但是,每次它运行时,我都会遇到大量与缩进和无效语法相关的错误(甚至像 ''' 这样的多行注释会触发“无效语法”在第 234 行“错误。

我试过将 IDE 切换到 sublime,甚至退格所有缩进,然后重新制表,但无济于事。每次我修复一行中的“错误”时,都会在另一行中创建一个新错误。

我的代码在下面,请告诉我有关如何修复的任何想法。

@require_POST   
def pay(request):


if request.method == 'POST':
form = CustomerForm(request.POST)

if form.is_valid():
# If the form has been submitted...
# All validation rules pass


#get the customer by session'd customer_id
c = get_object_or_404(Customer, pk = request.session['customer_id'])

#assign shipping info from POST to the customer object
c.first_name = request.POST['first_name']
c.last_name = request.POST['last_name']
c.street_address = request.POST['street_address']
c.city = request.POST['city']
c.state = request.POST['state']
c.zip = request.POST['zip']

#assign email info from POST to the customer object

c.email_address = request.POST['email_address']

stripe.api_key = REDACTED

# Get the credit card details submitted by the form
token = request.POST['stripeToken']


#tries to save the newly added form data.
try:
#save the new customer object's data
c.save()

########## THIS HANDLES CREATING A NEW STRIPE PAYMENT ################

# Create a Customer
try:
customer = stripe.Customer.create(
card=token,
plan="monthly",
email= c.email_address)
#need to save customer's id (ex: c.stripe_id = token.id)

#if there's a token error
except stripe.error.InvalidRequestError, e:
pass

#if the card is declined by Stripe

except stripe.error.CardError, e:
body = e.json_body
err = body['error']

print "Status is: %s" % e.http_status
print "Type is: %s" % err['type']
print "Code is: %s" % err['code']
# param is '' in this case
print "Param is: %s" % err['param']
print "Message is: %s" % err['message']

except stripe.error.AuthenticationError, e:
# Authentication with Stripe's API failed
# (maybe you changed API keys recently)
pass

except stripe.error.APIConnectionError, e:

# Network communication with Stripe failed
pass

except stripe.error.StripeError, e:
# Display a very generic error to the user, and maybe send
# yourself an email
pass

except Exception, e:
# Something else happened, completely unrelated to Stripe
pass

return render(request, 'shipment/confirm.html', {'date' : 'April 15, 2014'})


#passes the context to the template for confirming the customer's data
#context = { 'email_address' : c.email_address, 'first_name' : c.first_name,
# 'last_name' : c.last_name, 'street_address' : c.street_address,
# 'city' : c.city, 'state' : c.state, 'zip' : c.zip, }

#return render(request, 'shipment/pay.html', context)

#If there is a duplicate email it redirects the user back to the form with no error message.




#If anything else happens, it redirects the user back to the form.
else:
form = CustomerForm() # An unbound form
return render(request, 'shipment/createAccount.html', { 'form': form } )

最佳答案

这是您在我的编辑器中的代码的几个屏幕截图,其中制表符(设置为 4)和空格字符以红色显示。如您所见,它在很多行上包含了两者的大杂烩。 Python 对空格非常敏感,因此保持一致很重要。这通常通过将您的编辑器配置为始终将制表符转换为 n 个空白字符来处理(反之亦然,但通常首选前者)。

要解决您的问题,请使用一种方法重新缩进所有内容。我的编辑器还有一个 convert-tabs-to-spaces 命令,可以先使用它来稍微简化任务。

first screenshot

second screenshot

关于python - 我将工作代码复制并粘贴到我的 IDE 中——现在 Python 抛出了大量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22691882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com