I want to create a graph with graphviz and python with a lot of nodes. But it renders on top of each other and I cant seem to figure out how to fix the spacing between graphs.
Here is the output:
And here is my code where I generate the nodes:
def build_mind_map(history, site_info):
mind_map = Graph('mind_map', filename='graph', engine='sfdp', strict=True)
mind_map.graph_attr = {'label': heading, 'labelloc': "t", "labelfontsize": '13.0', "labeljust": "l"}
mind_map.graph_attr.update(size="2,2")
sorted_list = sort_list(site_info)
nodes_list_0 = []
nodes_list_1 = []
g = sorted_list[0]
nodes_list_0.append(str(g[0]))
mind_map.attr('node', color='cyan4', style='filled', labelfontsize="20", pad='10')
mind_map.node(str(g[0]), label="<<font point-size='18'><B>" + g[0] + "</B></font><br/><br/>>", nodesep="2")
for j in sorted_list:
date_string = '\n'
for d in j[2]:
date_string = date_string + "\n" + d
if str(j[0]) not in nodes_list_0:
mind_map.attr('node', color='crimson', style='filled', pad='10')
mind_map.node(str(j[0]), label="<<B>" + j[0] + "</B><br/><br/>" + date_string + ">", nodesep="2")
nodes_list_0.append(str(j[0]))
if str(j[1]) not in nodes_list_1:
mind_map.attr('node', color='cyan3', style='filled', pad='10')
mind_map.node(str(j[1]), label="<<B>" + j[1] + "</B><br/><br/>" + date_string + ">")
nodes_list_1.append(str(j[1]))
mind_map.edge(str(j[0]), str(j[1]))
mind_map.edge(str(g[0]), str(g[1]))
if history == "1":
mind_map_second = Graph('mind_map', filename='graph', engine='sfdp')
mind_map_second.attr('node', color='yellow', style='filled', labelfontsize="20")
for i in db.get_history_matches():
m = i[3].split(",")
info_string = ""
try:
if len(m[0]) > 0:
info_string += "<br/>Branch: " + m[0]
except:
pass
try:
if len(m[1]) > 0:
info_string += "<br/>Driver: " + m[1]
except:
pass
try:
if len(m[2]) > 0:
info_string += "<br/>Location" + m[2]
except:
pass
try:
if len(m[3]) > 0:
info_string += "<br/>Date: " + m[3]
except:
pass
try:
if len(m[4]) > 0:
info_string += "<br/>CIS: " + m[4]
except:
pass
try:
if len(m[5]) > 0:
info_string += "<br/>CAS: " + m[5]
except:
pass
if str(i[0]) not in nodes_list_0:
mind_map_second.node(str(i[0]), label="<History Found<br/><br/><B>" + i[0] + "</B><br/>" + info_string + ">")
nodes_list_0.append(str(i[0]))
if str(i[1]) not in nodes_list_1:
mind_map_second.node(str(i[1]), label="<History Found<br/><br/><B>" + i[1] + "</B><br/>" + info_string + ">")
nodes_list_1.append(str(i[1]))
mind_map_second.edge(str(i[0]), str(i[1]))
mind_map.subgraph(mind_map_second)
mind_map.view()
Can someone please help me to figure out how to get the spacing sorted.
Thanks
Related
I am trying to create a simple encryption scheme. The scheme is working fine when I take input of the plain text in uppercase. But when I take input of the plain text in lowercase, it is not decrypted correctly.
Can anyone help me to find out the solution?
Here is my code:
def generateKey(string, key):
key = list(key)
st = list(string)
if len(string) == len(key):
return(key,st)
else:
x=len(string) -len(key)
if x>0:
for i in range(x):
key.append(key[i % len(key)])
else:
y=len(key)-len(string)
for j in range(y):
st.append("X")
return("" . join(key),"" . join(st))
def cipherText(string, key):
cipher_text = []
for i in range(len(string)):
if string[i]== " ":
x = ord(string[I])
i+=1
cipher_text.append(chr(x))
else:
abc = ord(string[i]) + ord(key[I])
x = (abc + 2) % 26
x += ord('A')
cipher_text.append(chr(x))
ci="" . join(cipher_text)
rev = ""
for i in ci:
rev = i + rev
return(rev)
def originalText(cipher_text, key):
orig_text = []
re_rev= ""
for i in cipher_text:
re_rev = i + re_rev
for i in range(len(re_rev)):
if re_rev[i]== " ":
x = ord(re_rev[I])
i+=1
orig_text.append(chr(x))
else:
abc = ord(re_rev[i]) - ord(key[i]) + 26
x = (abc - 2) % 26
x += ord('A')
orig_text.append(chr(x))
return("" . join(orig_text))
if __name__ == "__main__":
string = input("Enter Plain Text: ")
keyword = input("Enter Keyword: ")
key,stri = generateKey(string, keyword)
cipher_text = cipherText(stri,key)
print("Ciphertext :", cipher_text)
print("Original/Decrypted Text :",
originalText(cipher_text, key))
HOW IT SHOULD LOOK ALIKE
I want pattern to be like this, but I couldn't come up with the loops ((
rows = int(input("number of rows: "))
space = rows - 1
n = 5
for i in range(0, rows):
for j in range(0, space):
print("", end=" " * n)
for j in range(0, i + 1):
print("*", end=" " * n)
space = space - 1
print()
space = 1
for i in range(rows - 1, 0, +1):
for j in range(space, 0):
print(" ", end=" ")
for k in range(0, i - 1):
print("*", end=" " * n)
space = space + 1
print()
rows = int(input("number of rows: "))
width = int(input("width of your window: "))
space = 9 # space between stars
for i in range(rows):
string = " " * (i % space)
for j in range(width):
if j % space == 0:
string += "*"
else:
string += " "
print(string)
How do I complete this recursive function at output_string += ? Output should be: 5! = 5 * 4 * 3 * 2 * 1 = 120
def print_factorial(fact_counter, fact_value):
output_string = ''
if fact_counter == 0: # Base case: 0! = 1
output_string += '1'
elif fact_counter == 1: # Base case: print 1 and result
output_string += str(fact_counter) + ' = ' + str(fact_value)
else: # Recursive case
output_string += str(fact_counter) + ' * '
next_counter = fact_counter - 1
next_value = next_counter * fact_value
output_string +=
return output_string
user_val = 5
print('%d! = ' % user_val, end="")
print(print_factorial(user_val, user_val))
def factorial_str(fact_counter, fact_value):
output_string = ''
if fact_counter == 0: # Base case: 0! = 1
output_string += '1'
elif fact_counter == 1: # Base case: print 1 and result
output_string += str(fact_counter) + ' = ' + str(fact_value)
else: # Recursive case
output_string += str(fact_counter) + ' * '
next_counter = fact_counter - 1
next_value = next_counter * fact_value
output_string += factorial_str(next_counter, next_value)
return output_string
user_val = int(input())
print('{}! = '.format(user_val), end="")
print(factorial_str(user_val, user_val))
Using the above answer I was able to work out the code. I have posted the answer that passed the case test for me.
def print_factorial(fact_counter, fact_value):
output_string = ''
if fact_counter == 0: # Base case: 0! = 1 #This line will never execute, no recursive call will ever be made with fact_counter = 0 and there shouldn't be anyways, so its harmless.
output_string += '1'
elif fact_counter == 1: # Base case: print 1 and result
output_string += str(fact_counter) + ' = ' + str(fact_value)
else: # Recursive case
output_string += str(fact_counter) + ' * '
next_counter = fact_counter - 1
next_value = next_counter * fact_value
output_string += print_factorial(next_counter, next_value)
return output_string
user_val = 5
print('%d! = ' % user_val, end="")
print(print_factorial(user_val, user_val))
You needed to compute the next number in the factorial then add it with "*" too. So a recursive call should be made at the concatenation statement.
Umm, if fact_counter == 0, will NEVER execute and I've written a comment to point this out inside the code.
I am making a quiz which can work with different difficulties and it works fine. The only thing is that its like my code ignores my if statement at the bottom. even when the variable 'w' = 9, which is when i have answered 9 questions, it still doesn't print the statement it just continues looping.
import csv
w = 0
score = 0
global q
with open("Computing.txt", "r") as file:
reader = csv.reader(file)
for row in reader:
dif_c = str(dif) + " "
if dif_c + str(q) + ")" in row[0]:
print (row[0].split(dif_c)[1])
print (row[1])
if dif == 1:
print (row[2])
input10 = input("Answer 'a' or 'b': ")
elif dif == 2:
print(row[2] + "\n" + row[3])
input10 = input("Answer 'a','b' or 'c': ")
elif dif == 3:
print(row[2] + "\n" + row[3] + "\n" + row[4])
input10 = input("Answer 'a','b','c' or 'd': ")
if input10 == row[dif + 2]:
print("Correct")
score = score + 1
w = w + 1
elif input10 != row[dif + 2]:
print("Incorrect")
w = w + 1
if w == 9:
print("Game over")
print("You got", r, "right out of 10")
while True:
quiz()
this is all the quiz function and i defined w and score as 0 within the function which i know wouldnt work but i have no clue how to fix it
Your 'if' condition to check 'w == 9' should be inside the 'for' loop and you need to break out of the 'for' loop based on that condition. Otherwise it will continue to loop.
Currently your 'if' check is outside the 'for' loop.
So it should be changed to something like this:
import csv
w = 0
score = 0
global q
with open("Computing.txt", "r") as file:
reader = csv.reader(file)
for row in reader:
dif_c = str(dif) + " "
if dif_c + str(q) + ")" in row[0]:
print (row[0].split(dif_c)[1])
print (row[1])
if dif == 1:
print (row[2])
input10 = input("Answer 'a' or 'b': ")
elif dif == 2:
print(row[2] + "\n" + row[3])
input10 = input("Answer 'a','b' or 'c': ")
elif dif == 3:
print(row[2] + "\n" + row[3] + "\n" + row[4])
input10 = input("Answer 'a','b','c' or 'd': ")
if input10 == row[dif + 2]:
print("Correct")
score = score + 1
w = w + 1
elif input10 != row[dif + 2]:
print("Incorrect")
w = w + 1
if w == 9:
print("Game over")
print("You got", r, "right out of 10")
break
I was taking a online class for Python and one of the tutorials is to create a battle system for a RPG. I finished the tutorial and felt that there was so much missing. One of the missing features was there is no option to go back to a players action which can lead to an infinite loop. I have coded a large section to include a way to go back by typing '0' and it works except for when there is no quantity for an item when selecting items and when there is no more MP for a spell. What happens in that case is when '0' is selected, the last entry on the array is automatically implemented. So lets say I have no MP and 'cura' is my last spell on the list, if I select '0' cura gets cased regardless. Can anyone tak a look at this small section I have included to see where the error is? I will include the entire code if needed.
while running:
print("====================\n")
print("NAME HP MP")
for player in players:
player.get_stats()
print("")
for enemy in enemies:
enemy.get_enemy_stats()
for player in players:
# check if player won
if defeated_enemies == 3:
print("\n" + bcolors.OKGREEN + "You Win!\n♫♫ Fanfare ♫♫" + bcolors.ENDC)
running = False
break
start = True
while start:
start = False
choice = player.choose_action()
index = choice - 1
if index == 0:
dmg = player.generate_damage()
enemy = player.choose_target(enemies)
enemies[enemy].take_damage(dmg)
print("\n" + player.name.replace(" ", "") + " attacked " + enemies[enemy].name.replace(" ", "") + " for", dmg, "points of damage.")
if enemies[enemy].get_hp() == 0:
print(bcolors.BOLD + bcolors.FAIL + enemies[enemy].name.replace(" ", "") + " HAS FALLEN!" + bcolors.ENDC)
del enemies[enemy]
defeated_enemies += 1
elif index == 1:
check_mp = True
while check_mp:
mag_choice = player.choose_magic()
magic_choice = mag_choice - 1
spell = player.magic[magic_choice]
magic_dmg = spell.generate_damage()
current_mp = player.get_mp()
if spell.cost > current_mp:
print(bcolors.FAIL + "\nNot enough MP. Please choose a different spell. Type 0 to go back." + bcolors.ENDC)
continue
else:
check_mp = False
if magic_choice == -1:
start = True
continue
player.reduce_mp(spell.cost)
if spell.type == "white":
player.heal(magic_dmg)
print(bcolors.OKBLUE + "\n" + spell.name + " heals", player.name, "for", str(magic_dmg), "HP." + bcolors.ENDC)
if player.hp > player.maxhp:
player.hp = player.maxhp
elif spell.type == "black":
enemy = player.choose_target(enemies)
enemies[enemy].take_damage(magic_dmg)
print(bcolors.OKBLUE + "\n" + spell.name + " deals", str(magic_dmg), "points of damage to " + enemies[enemy].name.replace(" ", "") + bcolors.ENDC)
if enemies[enemy].get_hp() == 0:
print(bcolors.BOLD + bcolors.FAIL + enemies[enemy].name.replace(" ", "") + " HAS FALLEN!" + bcolors.ENDC)
del enemies[enemy]
defeated_enemies += 1
elif index == 2:
choose_item = True
while choose_item:
itm_choice = player.choose_item()
item_choice = itm_choice -1
item = player.items[item_choice]["item"]
if player.items[item_choice]["quantity"] == 0:
print(bcolors.FAIL + "\nThere are no more " + str(item.name) +"s left. Please choose a different item. Type 0 to go back." + bcolors.ENDC)
continue
else:
break
if item_choice == -1:
start = True
continue
player.items[item_choice]["quantity"] -= 1
if item.type == "potion":
player.heal(item.prop)
print(bcolors.OKGREEN + "\n" + item.name + " heals", player.name, "for", str(item.prop), "HP." + bcolors.ENDC)
if player.hp > player.maxhp:
player.hp = player.maxhp
elif item.type == "elixer":
if item.name == "MegaElixer":
print("\n" + player.name.replace(" ", ""), "uses", item.name)
print(bcolors.OKGREEN + "HP and MP fully restored to all party members!" + bcolors.ENDC)
for i in players:
i.hp = i.maxhp
i.mp = i.maxmp
else:
player.hp = player.maxhp
player.mp = player.maxmp
print(player.name.replace(" ", ""), "uses", item.name)
print(bcolors.OKGREEN + player.name.replace(" ", "") + " HP and MP fully restored!" + bcolors.ENDC)
elif item.type == "attack":
enemy = player.choose_target(enemies)
enemies[enemy].take_damage(item.prop)
print(bcolors.FAIL + "\n" + item.name + " deals", str(item.prop), "points of damage to " + enemies[enemy].name.replace(" ", "") + bcolors.ENDC)
if enemies[enemy].get_hp() == 0:
print(bcolors.BOLD + bcolors.FAIL + enemies[enemy].name.replace(" ", "") + " HAS FALLEN!" + bcolors.ENDC)
del enemies[enemy]
defeated_enemies += 1
else:
continue