簡體   English   中英

Django Python str()參數2必須是str,而不是int

[英]Django Python str() argument 2 must be str, not int

我有一本12x12字典,其值全部為0。

matchfield = {}
    for i in range(12):
        for j in range(12):
            matchfield[str((i, j))] = 0

我想使用以下代碼段將一些值設置為1(它檢查周圍的字段是否空閑):

length = 4
m = randint(1, 10-length)
n = randint(1, 10)
for x in range(m-1, m+length+1):
    for y in range(n-1, n+1):
        if not matchfield[str((x, y))]:
            for k in range(length):
                matchfield[str((m+k, n))] = 1

如果我在python控制台中對此進行了測試,那么所有工作正常且所選的4個值都設置為1,但是在我的Django視圖函數中,我在下一行得到TypeError:

matchfield [str((m + k,n))] = 1

Environment:


Request Method: GET
Request URL: https://www.maik-kusmat.de/schiffeversenken/start/

Django Version: 1.11.5
Python Version: 3.5.3
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'django.contrib.flatpages',
 'accounts',
 'home',
 'contact',
 'kopfrechnen',
 'braces',
 'ckeditor',
 'ckeditor_uploader',
 'battleship',
 'hangman']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware']



Traceback:

File "/home/pi/Dev/mkenergy/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  41.             response = get_response(request)

File "/home/pi/Dev/mkenergy/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/home/pi/Dev/mkenergy/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/pi/Dev/mkenergy/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "/home/pi/Dev/mkenergy/src/battleship/views.py" in battleship_start
  36.                                 matchfield[str((m+k, n))] = 1

Exception Type: TypeError at /schiffeversenken/start/
Exception Value: str() argument 2 must be str, not int

我錯過了什么? 我不明白錯誤

我希望argument 2 must be str錯誤,如果您有

matchfield[str(m+k, n)]

在Python 3中, str的第二個參數是編碼,因此整數n會導致該錯誤。

但是,您的回溯顯示matchfield[str((m+k, n))] ,不會引起該錯誤。 嘗試重新啟動Django服務器,以確保您正在運行當前代碼。

首先,我建議您使用元組作為字典鍵,例如

matchfield[(i, j)] = 0

但是,如果將matchfield序列matchfield json,則將無法正常工作,因為鍵必須是字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM