|
import urllib.request #this loads a library you will need. Put this line at the top of your file.
def readfile():
case =[]
response = urllib.request.urlopen("http://research.cs.queensu.ca/home/cords2/treasure.txt")
html = response.readline().decode('utf-8')
while len(html) > 0:
html = response.readline().decode('utf-8')
case.append(html.strip("\n"))
return case
def process_list(case):
for i in range(len(case)-1):
if case[0] in ["0","1", "2", "3", "4", "5", "6", "7", "8", "9"]:
case = int(case)
case.remove('') # I don't know why but it dose have a '' at the end of the list
return case
def put_in_bag(thing, bag = {}):
bag[thing] = 1
return bag
def ask_for_input(case):
print("Please choose a number from {} to {}".format(0, len(case)-1))
user_input = int(input("Enter here: "))
return user_input
def what_behind(tidy_case, user_input):
thing = tidy_case[user_input]
return thing
def increment(bag, thing,):
bag[thing] += 1
return bag
def main():
signal = True
case = readfile()
tidy_case = process_list(case)
print(tidy_case)
while signal:
user_input = ask_for_input(tidy_case)
thing = what_behind(tidy_case, user_input)
try:
while type(thing) == int:
thing = what_behind(tidy_case, thing)
bag = put_in_bag(thing)
bag[thing] += 1
print("Wow, you get a {}, the {} is put in your backpack.".format(thing, thing))
except IndexError:
print("You are out of the map now!!")
print("All you belongs and points are zero now")
signal = False
finally:
print("here is item {}, and number {}".format(thing, bag[thing]))
print(bag)
main()
这个大概就是玩家随意输入一个数字,然后到达指定位置,若果有宝藏,东西将放入背包,如果重复是一件宝藏,数量则增加。但我现在每次loop完,会被bag = put_in_bag(thing)重置背包,不知道该怎么改了
|
|