![](/img/trans.png)
[英]Select lines having count greater than x and less than y using linux command
[英]if x > y not working where x is greater than y python 2.7
我試圖在不帶pygame和curses的Linux終端上制作游戲。 我制作了一個名為readlevels()
的函數,並在第36和39行出現了一個錯誤。 程序的那部分是確保所有級別都適合終端。 我什至使用print
來確保變量是正確的。 有人知道為什么它不起作用嗎? 我使用的是python 2.7,並且級別存儲在.txt文件中,但我懷疑這樣做是否有幫助。
import sys, os, time
def readlevels():
#Open level file
levelfile = 'levels.txt' #file with all the levels
try:
leveltxt = open(levelfile, 'r')
except IOError:
return str('The file ' + levelfile + ' is not in this directory. Please look for a file with the levels and name it' +levelfile + ' before continuing the game. This file is needed to store all of the levels$
#Read level file
levels = []
currentlevel = []
for line in leveltxt.readlines():
if ';' in line: #lines with ';' will be ignored
pass
elif '~' in line: #lines with '~' will seperate levels (also show level number)
levels.append(currentlevel)
currentlevel = []
else: #all other lines are part of the level
currentlevel.append(line)
#Make Necessary adjustments in the list 'levels' and make the cleaner list 'levels2'
levels = levels[1:] #gets rid of the [] at the beginning of 'levels' (bug fix)
levels2 = []
for level in levels:
level2 = [] #not to be confused with 'levels2' which has an 's'. This replaces all items in 'levels' while 'levels2' replaces the entire list
for line in level:
level2.append(line.rstrip())
levels2.append(level2)
#Overwrite 'levels' with 'levels2' so it does not look weird for the rest of the program
levels = levels2
#Checks to see if all levels will fit on the terminal
terminalsize = os.popen('stty size', 'r').read().split() #terminalsize[0] is rows in terminal, terminalsize[1] is letters per row
print terminalsize
levelnum = 1 #level number
for level in levels: #level is now each level again
print len(level), terminalsize[0] #this was to check if len(level) and terminalsize[0] was the number I wanted
if len(level) > terminalsize[0]: #if level has more rows than the terminal
return str('Level ' + levelnum + ' is too big for the terminal. Easily fixable by increasing the size of the terminal or decreasing the font size, or deleting/changing the level')
for line in level:
if len(line) > terminalsize[1]: #if level is too wide for the terminal
return str('Level ' + levelnum + ' is too big for the terminal. Easily fixable by increasing the size of the terminal, decreasing the font size, or deleting/changing the level')
levelnum += 1
#Finish
return levels
print readlevels()
以下代碼是levels.txt
,但我懷疑它會有所幫助。
-Blocks --------- P
-Enemies -------- A, B, C, D, E
-Exit Block ----- S
-Start Block ---- S
~1
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
P P
P P
P P
P P
P P
P EEEE P
P E E P
P E E P
P EEEE P
P P
P P
P P
P P
P P
P P
P P
P P
P P
P P
P P
P P
P P
P P
P P
P P
P P
P S P
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
~2
- TO BE CONTD
“讀取級別”通常返回所有級別的列表,並且列表中的每個級別都是該級別中所有行的另一個列表。
看起來您正在將整數與字符串進行比較。 嘗試
if len(level) > int(terminalsize[0]):
代替。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.