A compass for a textadventure - python

I am trying to program an ascii compass for a text adventure. I found something like I want to do at this url:
https://codereview.stackexchange.com/questions/207997/map-generator-for-a-text-based-role-playing-game
I did this modification for me:
north = "The Hills"
west = "A realy dark room"
#west = "A longer text destroy the positions"
east = "Entrance to the Dungeon"
south = "[X]"
n = "N"
s = "S"
vert_line = "║"
hzt_line = " ⯇ W ══ (C) ══ E ⯈ "
print(north.center(72, " "))
print(r"▲".rjust(len(west)+10))
print(r""+n.rjust(len(west)+10))
print(r""+vert_line.rjust(len(west)+10))
print(r""+vert_line.rjust(len(west)+10))
print(west.rjust(len(east)) + hzt_line + east)
print(r""+vert_line.rjust(len(west)+10))
print(r""+vert_line.rjust(len(west)+10))
print(r""+s.rjust(len(west)+10))
print(r"▼".rjust(len(west)+10))
print(south.center(len(west)+len(hzt_line)+len(east)-5, " "))
The problem is the calculation of the correct whitespace for the labels. It depends on the length of the text and breaks out of the center.
I'm also stuck at the calculation for the whitespace .. the result should be like this:
This is up
▲
N
|
To the left ◀ W ── [C] ── E ⯈ Dining Room
|
S
▼
Kitchen
The code looks also really ugly, maybe anyone knows a smarter solution.
Thank you very much for help!
Rufnex

I ran into a problem playing with your code. For some reason the left arrow graphic messes up the centering. When I changed the character, this worked well:
north = "The Hills"
west = "A realy dark room"
east = "Entrance to the Dungeon"
south = "[X]"
n = "N"
s = "S"
vert_line = "║"
#hzt_line = " ⯇ W ══ (C) ══ E ⯈ " # for some reason the left arrow graphic messes up the centering
hzt_line = " < W == (C) == E > "
# synchronize east/west lengths and get line length
labelsize= max(len(west),len(east))
westpad = west.rjust(labelsize)
eastpad = east.ljust(labelsize)
long_line = westpad+hzt_line+eastpad
line_length = len(long_line)
# print compass
print(north.center(line_length))
print(r"▲".center(line_length))
print(n.center(line_length))
print(vert_line.center(line_length))
print(vert_line.center(line_length))
print(long_line.center(line_length))
print(vert_line.center(line_length))
print(vert_line.center(line_length))
print(s.center(line_length))
print(r"▼".center(line_length))
print(south.center(line_length))

Related

Depth first search algorithm skipping spaces in maze?

After concluding the first lecture of Harvard's AI course on edX, I have decided to implement the concepts taught, first being the depth-first search algorithm.
The objective of this program is to input a maze in text file mazefile and find a path from S to G using the depth-first search algorithm.
The project currently consists of 4 files, (1) the code with the class methods to operate or use the (2) text file which contains the maze, another text file (3) that contains the result file (where the AI has explored) and the main python script (4). Here they are, feel free to copy and paste these into a folder and to see how they run.
processText.py (file 1)
#code to process the mazefile file.
class importMaze:
def __init__(self,maze):
self.fileLines = []
self.fileName = maze
self.switch = False
self.toBeReturned = []
def processThis(self):
f = open(self.fileName,"r")
for x in f:
self.fileLines.append(x[:-1])
f.close()
for i in self.fileLines:
if self.switch == True:
if str(i) == "END":
self.switch = False
else:
self.toBeReturned.append(i)
else:
if str(i) == "START":
self.switch = True
return self.toBeReturned
class mazePointer:
def __init__(self,mazearray):
self.Sample = mazearray
self.initialPosition = []
for y in range(0, len(self.Sample)):
for x in range(0,len(self.Sample[y])):
if str(self.Sample[y][x]) == "S":
self.initialPosition = [x,y]
self.currentPosition = self.initialPosition
def whatIs(self,xcoordinate,ycoordinate):
return (self.Sample[ycoordinate])[xcoordinate]
def nearbyFreeSpaces(self,search):
self.freeSpaces = []
if self.whatIs(self.currentPosition[0]-1,self.currentPosition[1]) == search:
self.freeSpaces.append([self.currentPosition[0]-1,self.currentPosition[1]])
if self.whatIs(self.currentPosition[0]+1,self.currentPosition[1]) == search:
self.freeSpaces.append([self.currentPosition[0]+1,self.currentPosition[1]])
if self.whatIs(self.currentPosition[0],self.currentPosition[1]-1) == search:
self.freeSpaces.append([self.currentPosition[0],self.currentPosition[1]-1])
if self.whatIs(self.currentPosition[1],self.currentPosition[1]+1) == search:
self.freeSpaces.append([self.currentPosition[1],self.currentPosition[1]+1])
return self.freeSpaces
def moveTo(self,position):
self.currentPosition = position
TestingTrack.py (the main file)
from processText import importMaze, mazePointer
testObject = importMaze("mazefile")
environment = testObject.processThis()
finger = mazePointer(environment)
frontier = []
explored = []
result = ""
def Search():
global result
if len(finger.nearbyFreeSpaces("G")) == 1: #If the goal is bordering this space
result = finger.nearbyFreeSpaces("G")[0]
explored.append(finger.currentPosition)
else:
newPlaces = finger.nearbyFreeSpaces("F") #finds the free spaces bordering
for i in newPlaces:
if i in explored: #Skips the ones already visited
pass
else:
frontier.append(i)
while result == "":
explored.append(finger.currentPosition)
Search()
finger.moveTo(frontier[-1])
frontier.pop(-1)
exploredArray = []
for y in range(len(environment)): #Recreates the maze, fills in 'E' in where the AI has visited.
holder = ""
for x in range(len(environment[y])):
if [x,y] in explored:
holder+= "E"
else:
holder+= str(environment[y][x])
exploredArray.append(holder)
def createResult(mazeList,title,append): #Creating the file
file = open("resultfile",append)
string = title + " \n F - Free \n O - Occupied \n S - Starting point \n G - Goal \n E - Explored/Visited \n (Abdulaziz Albastaki 2020) \n \n (top left coordinate - 0,0) \n "
for i in exploredArray:
string+= "\n" + str(i)
string+= "\n \n Original problem \n"
for i in environment:
string+= "\n" +str(i)
file.write(string)
file.close()
def tracingPath():
initialExplored = explored
proceed = True
newExplored = []
for i in explored:
finger.moveTo() #incomplete
print(explored)
createResult(exploredArray,"DEPTH FIRST SEARCH", "w")
mazefile (the program will read this file to get the maze)
F - Free
O - Occupied
S - Starting point
G - Goal
(Abdulaziz Albastaki 2020)
START
OOOOOOOOOOOOOOOO
OFFFFFFFFFFFFFGO
OFOOOOOOOOOOOOFO
OFOOOOOOOOOOOOFO
OFOOOOOOOOOOOOFO
OFOOOOOOOOOOOOFO
OSFFFFFFFFFFFFFO
OOOOOOOOOOOOOOOO
END
Made by Abdulaziz Albastaki in October 2020
You can change the maze and its size however it must
-Respect the key above
-Have ONE Starting point and goal
-The maze must be in between 'START' and 'END'
-The maze MUST be surrounded by occupied space
SAMPLE PROBLEMS:
OOOOOOOOOOOOOOOO
OFFFFFFFFFFFFFGO
OFOOOOOOOOOOOOFO
OFOOOOOOOOOOOOFO
OFOOOOOOOOOOOOFO
OFOOOOOOOOOOOOFO
OSFFFFFFFFFFFFFO
OOOOOOOOOOOOOOOO
OOOOOOOOOOOOOOOOO
OFOFFFFFOOOFFFOOO
OFFFOOOFOOFFOOOFO
OFOOOOOFOOFOOOOFO
OSFGFFFFFFFFFFFFO
OOOOOOOOOOOOOOOOO
There is also a resultfile, however if you would just create an empty textfile with that name (no extension), the program will fill it in with results.
The problem is with the resultfile, here it is:
DEPTH FIRST SEARCH
F - Free
O - Occupied
S - Starting point
G - Goal
E - Explored/Visited
(Abdulaziz Albastaki 2020)
(top left coordinate - 0,0)
OOOOOOOOOOOOOOOO
OFFFFFFFFFFFFFGO
OFOOOOOOOOOOOOEO
OFOOOOOOOOOOOOEO
OFOOOOOOOOOOOOEO
OEOOOOOOOOOOOOEO
OEFFFEEEEEEEEEEO
OOOOOOOOOOOOOOOO
Original problem
OOOOOOOOOOOOOOOO
OFFFFFFFFFFFFFGO
OFOOOOOOOOOOOOFO
OFOOOOOOOOOOOOFO
OFOOOOOOOOOOOOFO
OFOOOOOOOOOOOOFO
OSFFFFFFFFFFFFFO
OOOOOOOOOOOOOOOO
The AI skipped a few spaces to get to the goal, why is it doing so?
Feel free to ask me for any clarifications.
There are the following issues:
the last if block in nearbyFreeSpaces uses a wrong index:
if self.whatIs(self.currentPosition[1],self.currentPosition[1]+1) == search:
self.freeSpaces.append([self.currentPosition[1],self.currentPosition[1]+1])
should be:
if self.whatIs(self.currentPosition[0],self.currentPosition[1]+1) == search:
self.freeSpaces.append([self.currentPosition[0],self.currentPosition[1]+1])
The final position is not correctly added to the path. The last line of this block:
if len(finger.nearbyFreeSpaces("G")) == 1: #If the goal is bordering this space
result = finger.nearbyFreeSpaces("G")[0]
explored.append(finger.currentPosition)
...should be:
explored.append(result)

BeautifulSoup trying to get text from wrapped divs but empty or "none" is being returned

Here is a picture (sorry) of the HTML that I am trying to parse:
I am using this line:
home_stats = soup.select_one('div', class_='statText:nth-child(1)').text
Thinking that I'd get the 1st child of the class statText and the outcome would be 53%.
But it's not. I get "Loading..." and none of the data that I was trying to use and display.
The full code I have so far:
soup = BeautifulSoup(source, 'lxml')
home_team = soup.find('div', class_='tname-home').a.text
away_team = soup.find('div', class_='tname-away').a.text
home_score = soup.select_one('.current-result .scoreboard:nth-child(1)').text
away_score = soup.select_one('.current-result .scoreboard:nth-child(2)').text
print("The home team is " + home_team, "and they scored " + home_score)
print()
print("The away team is " + away_team, "and they scored " + away_score)
home_stats = soup.select_one('div', class_='statText:nth-child(1)').text
print(home_stats)
Which currently does print the hone and away team and the number of goals they scored. But I can't seem to get any of the statistical content from this site.
My output plan is to have:
[home_team] had 53% ball possession and [away_team] had 47% ball possession
However, I would like to remove the "%" symbols from the parse (but that's not essential). My plan is to use these numbers for more stats later on, so the % symbol gets in the way.
Apologies for the noob question - this is the absolute beginning of my Pythonic journey. I have scoured the internet and StackOverflow and just can not find this situation - I also possibly don't know exactly what I am looking for either.
Thanks kindly for your help! May your answer be the one I pick as "correct" ;)
Assuming that this is the website that u r tryna scrape, here is the complete code to scrape all the stats:
from bs4 import BeautifulSoup
from selenium import webdriver
import pandas as pd
driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://www.scoreboard.com/en/match/SO3Fg7NR/#match-statistics;0')
pg = driver.page_source #Gets the source code of the page
driver.close()
soup = BeautifulSoup(pg,'html.parser') #Creates a soup object
statrows = soup.find_all('div',class_ = "statTextGroup") #Finds all the div tags with class statTextGroup -- these div tags contain the stats
#Scrapes the team names
teams = soup.find_all('a',class_ = "participant-imglink")
teamslst = []
for x in teams:
team = x.text.strip()
if team != "":
teamslst.append(team)
stats_dict = {}
count = 0
for x in statrows:
txt = x.text
final_txt = ""
stat = ""
alphabet = False
percentage = False
#Extracts the numbers from the text
for c in txt:
if c in '0123456789':
final_txt+=c
else:
if alphabet == False:
final_txt+= "-"
alphabet = True
if c != "%":
stat += c
else:
percentage = True
values = final_txt.split('-')
#Appends the values to the dictionary
for x in values:
if stat in stats_dict.keys():
if percentage == True:
stats_dict[stat].append(x + "%")
else:
stats_dict[stat].append(int(x))
else:
if percentage == True:
stats_dict[stat] = [x + "%"]
else:
stats_dict[stat] = [int(x)]
count += 1
if count == 15:
break
index = [teamslst[0],teamslst[1]]
#Creates a pandas DataFrame out of the dictionary
df = pd.DataFrame(stats_dict,index = index).T
print(df)
Output:
Burnley Southampton
Ball Possession 53% 47%
Goal Attempts 10 5
Shots on Goal 2 1
Shots off Goal 4 2
Blocked Shots 4 2
Free Kicks 11 10
Corner Kicks 8 2
Offsides 2 1
Goalkeeper Saves 0 2
Fouls 8 10
Yellow Cards 1 0
Total Passes 522 480
Tackles 15 12
Attacks 142 105
Dangerous Attacks 44 29
Hope that this helps!
P.S: I actually wrote this code for a different question, but I didn't post it as an answer was already posted! But I didn't know that it would come in handy now! Anyways, I hope that my answer does what u need.

How to solve Einsteins riddle with z3py the elegant way?

z3py guys have provided a code what is based here https://github.com/0vercl0k/z3-playground/blob/master/einstein_riddle_z3.py . However comparing to this https://artificialcognition.github.io/who-owns-the-zebra the solution is rather complicated, long and ugly. I do not really want to switch the libraries as z3py seems more advanced and maintained. So I started to work on my version, but I fail to declare some parts (lack of knowledge or not possible?). Here is what I have and where I get stuck (2 comments):
from z3 import *
color = Int('color')
nationality = Int('nationality')
beverage = Int('beverage')
cigar = Int('cigar')
pet = Int('pet')
house = Int('house')
color_variations = Or(color==1, color==2, color==3, color==4, color==5)
nationality_variations = Or(nationality==1, nationality==2, nationality==3, nationality==4, nationality==5)
beverage_variations = Or(beverage==1, beverage==2, beverage==3, beverage==4, beverage==5)
cigar_variations = Or(cigar==1, cigar==2, cigar==3, cigar==4, cigar==5)
pet_variations = Or(pet==1, pet==2, pet==3, pet==4, pet==5)
house_variations = Or(house==1, house==2, house==3, house==4, house==5)
s = Solver()
s.add(color_variations)
s.add(nationality_variations)
s.add(beverage_variations)
s.add(cigar_variations)
s.add(pet_variations)
s.add(house_variations)
# This is not right
#s.add(Distinct([color, nationality, beverage, cigar, pet]))
s.add(And(Implies(nationality==1, color==1), Implies(color==1, nationality==1))) #the Brit (nationality==1) lives in the red (color==1) house
s.add(And(Implies(nationality==2, pet==1), Implies(pet==1, nationality==2))) #the Swede (nationality==2) keeps dogs (pet==1) as pets
s.add(And(Implies(nationality==3, beverage==1), Implies(beverage==1, nationality==3))) #the Dane (nationality==3) drinks tea (beverage=1)
s.add(And(Implies(color==2, beverage==2), Implies(beverage==2, color==2))) #the green (color==2) house's owner drinks coffee (beverage==2)
s.add(And(Implies(cigar==1, pet==2), Implies(pet==2, cigar==1))) #the person who smokes Pall Mall (cigar==1) rears birds ([pet==2])
s.add(And(Implies(color==4, cigar==2), Implies(cigar==2, color==4))) #the owner of the yellow (color==4) house smokes Dunhill (cigar==2)
s.add(And(Implies(house==3, beverage==3), Implies(beverage==3, house==3))) #the man living in the center (hause==3) house drinks milk (beverage==3)
s.add(And(Implies(nationality==4, house==1), Implies(house==1, nationality==4))) #the Norwegian (nationality==4) lives in the first house (house==1)
s.add(And(Implies(cigar==3, beverage==4), Implies(beverage==4, cigar==3))) #the owner who smokes BlueMaster (cigar==3) drinks beer (beverage==4)
s.add(And(Implies(nationality==5, cigar==4), Implies(cigar==4, nationality==5))) #the German (nationality==5) smokes Prince (cigar==4)
# I can't figure our this part, so I can keep it short and efficient
# the green (color==2) house is on the left of the white (color==3) house
Currently looking into direction of ForAll and Functions
You should use an enumeration for the different kinds of things here. Also, you can't just get away with having one color variable: After all, each house has a different color, and you want to track it separately. A better idea is to make color, nationality, etc., all uninterpreted functions; mapping numbers to colors, countries, etc., respectively.
Here's the Haskell solution for this problem, using the SBV library which uses z3 via the SMTLib interface, following the strategy I described: https://hackage.haskell.org/package/sbv-8.8/docs/src/Documentation.SBV.Examples.Puzzles.Fish.html
Translating this strategy to Python, we have:
from z3 import *
# Sorts of things we have
Color , (Red , Green , White , Yellow , Blue) = EnumSort('Color' , ('Red' , 'Green' , 'White' , 'Yellow' , 'Blue'))
Nationality, (Briton , Dane , Swede , Norwegian, German) = EnumSort('Nationality', ('Briton' , 'Dane' , 'Swede' , 'Norwegian', 'German'))
Beverage , (Tea , Coffee , Milk , Beer , Water) = EnumSort('Beverage' , ('Tea' , 'Coffee' , 'Milk' , 'Beer' , 'Water'))
Pet , (Dog , Horse , Cat , Bird , Fish) = EnumSort('Pet' , ('Dog' , 'Horse' , 'Cat' , 'Bird' , 'Fish'))
Sport , (Football, Baseball, Volleyball, Hockey , Tennis) = EnumSort('Sport' , ('Football', 'Baseball', 'Volleyball', 'Hockey' , 'Tennis'))
# Uninterpreted functions to match "houses" to these sorts. We represent houses by regular symbolic integers.
c = Function('color', IntSort(), Color)
n = Function('nationality', IntSort(), Nationality)
b = Function('beverage', IntSort(), Beverage)
p = Function('pet', IntSort(), Pet)
s = Function('sport', IntSort(), Sport)
S = Solver()
# Create a new fresh variable. We don't care about its name
v = 0
def newVar():
global v
i = Int("v" + str(v))
v = v + 1
S.add(1 <= i, i <= 5)
return i
# Assert a new fact. This is just a synonym for add, but keeps everything uniform
def fact0(f):
S.add(f)
# Assert a fact about a new fresh variable
def fact1(f):
i = newVar()
S.add(f(i))
# Assert a fact about two fresh variables
def fact2(f):
i = newVar()
j = newVar()
S.add(i != j)
S.add(f(i, j))
# Assert two houses are next to each other
def neighbor(i, j):
return (Or(i == j+1, j == i+1))
fact1 (lambda i : And(n(i) == Briton, c(i) == Red)) # The Briton lives in the red house.
fact1 (lambda i : And(n(i) == Swede, p(i) == Dog)) # The Swede keeps dogs as pets.
fact1 (lambda i : And(n(i) == Dane, b(i) == Tea)) # The Dane drinks tea.
fact2 (lambda i, j: And(c(i) == Green, c(j) == White, i == j-1)) # The green house is left to the white house.
fact1 (lambda i : And(c(i) == Green, b(i) == Coffee)) # The owner of the green house drinks coffee.
fact1 (lambda i : And(s(i) == Football, p(i) == Bird)) # The person who plays football rears birds.
fact1 (lambda i : And(c(i) == Yellow, s(i) == Baseball)) # The owner of the yellow house plays baseball.
fact0 ( b(3) == Milk) # The man living in the center house drinks milk.
fact0 ( n(1) == Norwegian) # The Norwegian lives in the first house.
fact2 (lambda i, j: And(s(i) == Volleyball, p(j) == Cat, neighbor(i, j))) # The man who plays volleyball lives next to the one who keeps cats.
fact2 (lambda i, j: And(p(i) == Horse, s(j) == Baseball, neighbor(i, j))) # The man who keeps the horse lives next to the one who plays baseball.
fact1 (lambda i : And(s(i) == Tennis, b(i) == Beer)) # The owner who plays tennis drinks beer.
fact1 (lambda i : And(n(i) == German, s(i) == Hockey)) # The German plays hockey.
fact2 (lambda i, j: And(n(i) == Norwegian, c(j) == Blue, neighbor(i, j))) # The Norwegian lives next to the blue house.
fact2 (lambda i, j: And(s(i) == Volleyball, b(j) == Water, neighbor(i, j))) # The man who plays volleyball has a neighbor who drinks water.
# Determine who owns the fish
fishOwner = Const("fishOwner", Nationality)
fact1 (lambda i: And(n(i) == fishOwner, p(i) == Fish))
r = S.check()
if r == sat:
m = S.model()
print(m[fishOwner])
else:
print("Solver said: %s" % r)
When I run this, I get:
$ python a.py
German
Showing that the fish-owner is German. I think your original problem had a different but similar set of constraints, you can easily use the same strategy to solve your original.
It's also instructional to look at the output of:
print(m)
in the sat case. This prints:
[v5 = 4,
v9 = 1,
v16 = 2,
v12 = 5,
v14 = 1,
v2 = 2,
v0 = 3,
v10 = 2,
v18 = 4,
v15 = 2,
v6 = 3,
v7 = 1,
v4 = 5,
v8 = 2,
v17 = 1,
v11 = 1,
v1 = 5,
v13 = 4,
fishOwner = German,
v3 = 4,
nationality = [5 -> Swede,
2 -> Dane,
1 -> Norwegian,
4 -> German,
else -> Briton],
color = [5 -> White,
4 -> Green,
1 -> Yellow,
2 -> Blue,
else -> Red],
pet = [3 -> Bird,
1 -> Cat,
2 -> Horse,
4 -> Fish,
else -> Dog],
beverage = [4 -> Coffee,
3 -> Milk,
5 -> Beer,
1 -> Water,
else -> Tea],
sport = [1 -> Baseball,
2 -> Volleyball,
5 -> Tennis,
4 -> Hockey,
else -> Football]]
Ignore all the assignments to vN variables, those are the ones we used internally for modeling purposes. But you can see how z3 mapped each of the uninterpreted functions to the corresponding values. For all of these, the value mapped is the number of the house to the corresponding value that satisfies the puzzle constraints. You can programmatically extract a full solution to the puzzle as needed by using the information contained in this model.

Function to generate random story returns always same output

I created a very simple story generator with Python based on this comic strip: https://xkcd.com/2243/
Each time that I run the script, it generates a new random story, however, if the user chooses to run it again by writing "y", the story generated is always the same. What am I doing wrong?
This is the code:
# a random spoiler generator based on this comic:
# https://xkcd.com/2243/
import random
# define the various options and choose randomly one
villain = random.choice(['Kyle Ren', 'Malloc', 'Darth Sebelius', 'Theranos', 'Lord Juul'])
friend = random.choice(['Kym Spacemeasurer','Teen Yoda','Dab Tweetdek', 'Yaz Progestin', 'TI-83'])
lightsaber = random.choice(['beige', 'ochre', 'mauve', 'aquamarine', 'taupe'])
superweapon = random.choice(['Sun Obliterator', 'Moonsquisher', 'World Eater', 'Planet Zester', 'Superconducting Supercollider'])
superpower = random.choice(['blowing up a planet with a bunch of beams of energy that combine into one', 'blowing up a bunch of planets with one beam of energy that splits into many', 'cutting a planet in half and smashing the halves together like two cymbals', "increasing the CO2 levels in a planet's atmosphere, causing rapid heating", 'triggering the end credits before the movies is done'])
old_enemy = random.choice(['Boba Fett', 'Salacious Crumb', 'The Space Slug', 'The Bottom Half of Darth Maul', 'YouTube Commenters'])
feat = random.choice(['a bow that shoots little lightsaber-headed arrows.', 'X-Wings and TIE Fighters dodging the giant letters of the opening crawl.', 'a Sith educational display that uses force lightning to demonstrate the dielectric breakdown of air.', 'Kylo Ren putting on another helmet over his smaller one.', 'a Sith car wash where the bristles on the brushes are little lightsabers.'])
father = random.choice(['Luke', 'Leia', 'Han', 'Obi-Wan', 'a random junk-trader'])
mother = random.choice(['Poe.', 'BB-8.', 'Amilyn Holdo.', 'Laura Dern.', 'a random junk-trader.', 'that one droid from the Jawa Sandcrawler that says "gonk".'])
# creates the parts of the story
intro = 'In this Star Wars movie, our heroes return to take on the First Order and new villain '
part_1 = '. With help from their new friend '
part_2 = ', Rey builds a new lightsaber with a '
part_3 = " blade, and they head out to confront the First Order's new weapon, the "
part_4 = ', a space station capable of '
part_5 = '. They unexpectedly join forces with their old enemy, '
part_6 = ', and destroy the superweapon in a battle featuring '
part_7 = "\n\nP.S. Rey's parents are "
part_8 = ' and '
# generates the story
def rsg():
print(intro + villain + part_1 + friend + part_2 + lightsaber + part_3 + superweapon + part_4 + superpower + part_5 + old_enemy + part_6 + feat + part_7 + father + part_8 + mother)
# asks user to generate another story or not
while True:
rsg()
while True:
user_input = input('Would you like to generate a new spoiler? [y,n]\n')
if user_input not in ('y', 'n'):
print('Please enter "y" for yes or "n" for no.')
continue
if user_input == 'y':
break
else:
print('Alright, bye!')
quit()
You're using random.choice only first time when initiating. Should be:
villains = ['Kyle Ren', 'Malloc', 'Darth Sebelius', 'Theranos', 'Lord Juul']
def rsg():
print(intro + random.choice(villains) ....
The variables are never updated, just computed at the start of the program.
Put all the random.choice lines in the rsg function and you will be good !

osmnx road connecting two city aren't identified (missing node of border)

I have an Dijsktra Algorithm that I apply on a graph that I get from open street map. It works fine with a single graph.
But when I compose a graph of two neighboring city, the same algorithm doesn't find any way between the graphs .. I noticied that road connecting two city weren't identified by any osmid. :-/
Did I missed some thing ?
class Pathfinding(nx.MultiDiGraph):
def __init__(self, incomin_graphe_data = None, **attr):
nx.MultiDiGraph.__init__(self,incomin_graphe_data,**attr)
def dijsktra(self,depart,final):
inf = float("inf")
F ,ds, s= set(), None ,None
D = {i: inf for i in self.nodes}
D[ depart ] = 0
pred = {}
priority_queue = [(0 , depart)]
while s != final:
ds, s = heapq.heappop(priority_queue)
F.add(s)
for y in self.neighbors(s):
w = self[s][y][0]['lenght']
dy = ds + w
if y not in F :
if D[y] > dy :
D[y] = dy
heapq.heappush(priority_queue,(dy,y))
pred[y] = s
path = [s]
while s != depart:
s = pred[s]
path.append(s)
return path
There is the Dijkstra that I use and the following type graph are MultiDiGraph.
add_Aubervilliers = "Aubervilliers, France"
Graphe_Aubervilliers = ox.graph_from_place(add_Aubervilliers, network_type='drive')
add_Saint_denis = " Saint denis, France "
Graphe_Saint_denis = ox.graph_from_place(add_Saint_denis, network_type = "drive")
Graph_Paris_Auber = nx.compose_all([ Graphe_Saint_denis, Graphe_Aubervilliers ])
I also tryied with the following commande
add_Saint_denis = " Saint denis, France "
Graphe_Saint_denis = ox.graph_from_adress(add_Saint_denis, infrastructure='way['highway']')
But it give the same problem... Did I missed something ?
All you need is to set truncate_by_edge to True.

Categories

Resources