python中否定for 在python中否定函式

2021-10-22 11:30:01 字數 1396 閱讀 4033

有沒有一種方法可以否定乙個函式,使它返回負數。在我的函式中,我有條件句,每個條件句都讓這個「烏龜」移動。有沒有一種方法可以否定這一點,所以烏龜的每乙個動作都是否定的。我說的是'狀況'。在def ttinterpret( program ):

interpret program as a tinyturtle language string.

program -- a tinyturtle string, possibly with string manipulation symbols

stor_pos = turtle.pos()

spec_index = 0

for ch in program:

if ch >= '0' and ch <= '9':

turtle.fd(int(ch) * 10)

elif ch == '

turtle.left(15)

elif ch == 'l':

turtle.left(90)

elif ch == '>':

turtle.right(15)

elif ch == 'r':

turtle.right(90)

elif ch == ' ':

pass

elif ch == '@':

# ttinterpret( program[:program.index(ch):-1] )

stor_pos = turtle.pos() #keeps track of when @ was used

spec_index = program.index( ch ) #returns the index of the most recent specified character

elif ch == '/':

fds_index = program.index( ch ) #returns last '/' index

ttinterpret( program[spec_index:fds_index - 1] )

# while ch != '/':

# ttinterpret( -program[ch::-1] )

elif ch == '!':

turtle.setpos(stor_pos)

elif ch == '=':

ttinterpret( program[:int(program.index(ch)):-1] ) #returns to start

ttinterpret( program[:int(program.index(ch)):1] ) #mirrors from start

else:

print("error: ", ch," is not supported by tinyturtle")

return

python終端中執行py檔案

在除錯或使用某些模組時,需要時刻改變一些變數的值,若寫成py檔案,則每次都要儲存,而很多測試的內容是沒必要的。故可以先執行一些配置環境的py檔案,然後在python終端即互動模式下,呼叫類及其方法來測。其實只要加入 i的選項即可,如 當前目錄有檔案detail.py,內容為具體的執行語句,不是類的定...

python中跨檔案引入 py檔案

在本地可以通過在pycharm裡將檔案目錄調整為sources root實現。但在伺服器裡,需要通過如下方式跨檔案呼叫.py檔案。程式結構如下 src mod1.py test1.py 若在程式test1.py中匯入模組mod1,則直接使用 import mod1 或from mod1 import...

在乙個py指令碼中呼叫另外乙個py指令碼中的類或函式

1.兩個檔案在同一目錄,直接import即可 2.兩個檔案在不同目錄 在匯入檔案的時候,python只搜尋當前指令碼所在的目錄,載入 entry point 入口指令碼執行目錄和sys.path中包含的路徑例如包的安裝位址。所以如果要在當前指令碼引用其他檔案,除了將檔案放在和指令碼同一目錄下,還有以...