這次的練手程式依然來自於knight lab的部落格文章five mini programming projects for the python beginner,任務的名字叫做textbased adventure game,也就是文字冒險遊戲
目標:我們的目標是完整的建立乙個文字小遊戲,這個小遊戲基於玩家的輸入讓玩家在不同的房間中移動,並且玩家可以知道當前房間的描述資訊。為了完成這個遊戲,我們需要定義可移動的方向,還要監控玩家已經移動的距離,她/他現在在那個房間裡,並且需要在房間周圍放置「牆壁」來做一下範圍限制。
需要用到的python知識:
- string(字串)
- variables(變數定義)
- input/output(輸入/輸出)
- if/else statements(if/else控制流語句)
- print(輸出語句)
- list(列表)
- integers(整數型別)
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# firstly , we need a room, maybe it's length is 10m, width is 10m , and has a wall
# that length 5m in the meddle of the room
# the room looks like below
## ————————————————————
# | x |
# | |
# | |
# | |
# |—————————— |
# | |
# | |
# | o |
# |———————————————————|
# x is the starting point , and o is the destination
# use a list of list to discribe a room
room = [(x,y) for x in range(10) for y in range(10)]
# define which positions are in wall
wall =
for position in room:
if position[0] == 0
or position[0] == 9:
elif position[1] == 0
or position[1] == 9:
elif position[1] == 4
and position[0] <= 4:
# define the function of how to move
defmove
(original_position, direction):
if direction == "r":
return original_position[0]+1, original_position[1]
elif direction == "l":
return original_position[0]-1, original_position[1]
elif direction == "u":
return original_position[0], original_position[1]-1
elif direction == "d":
return original_position[0], original_position[1]+1
#def mv_left(original_positin):
# return original_positin[0]-1, original_positin[1]
##def mv_up(original_positin):
# return original_positin[0], original_positin[1]-1
##def mv_down(original_positin):
# return original_positin[0], original_positin[1]+1
defadventure
(): original_position = (1,1)
destination = (1,8)
while
true:
if original_position == destination:
print
"congratulations, you got it!"
break
else:
direction = raw_input("which direction would you like to move?(r/l/u/d) ").strip().lower()
if direction in ['r', 'd', 'l', 'u']:
final_position = move(original_position, direction)
if final_position in wall:
print
"this direction is the wall"
print
"you are on (%d,%d) now." % (original_position[0], original_position[1])
continue
else:
original_position = final_position
print
"you are on (%d,%d) now." % (original_position[0], original_position[1])
continue
else:
print
"you must input r,l,u or d"
continue
if __name__ == "__main__":
adventure()
以上是編寫的text adventure 小指令碼 小程式練手(c )
給定乙個陣列input 如果陣列長度n為奇數,則將陣列中最大的元素放到 output 陣列最中間的位置,如果陣列長度n為偶數,則將陣列中最大的元素放到 output 陣列中間兩個位置偏右的那個位置上,然後再按從大到小的順序,依次在第乙個位置的兩邊,按照一左一右的順序,依次存放剩下的數。例如 inpu...
python練手小程式
python小白 usr bin env python coding utf 8 time 2019 11 5 8 53 author october file py 整數序列求和 n input 請輸入整數n sum 0 for i in range int n sum i 1print 1到n求...
邏輯練練手小程式
寫個練練手,自己增加要求增加寫,至上而下寫 小程式要求 1.使用者可能輸入內容中前後多按了空格進行去除 2.使用者不按規定輸入內容返還輸入,並給與警告提示 3.需要記錄使用者的資訊,輸入次數規定3 4.提示密碼錯誤,賬號錯誤,或者兩者都錯 4.獎品可選猜對了可以獲取2個 5.猜的機會有3次 6.拿了...