How can i paint over text instead of printing multiple times? - python

How can I make the program print the CTRL+C to go BACK once down?[Look pictures][Picture][1]
while h < math.inf:
time2 = time.strftime("[%H" + ":%M" + ":%S]")
console = colorama.Fore.WHITE + time2 + '' + defaultname
file = open("Accounts/Failed.txt", "a+")
file2 = open("Accounts/Success.txt", "a+")
x = random.randrange(0, 100)
f = generator()
if x <= 97:
print(console + colorama.Fore.RED + "[FAILED] " + "0x" + f + ' ETH Wallet' + colorama.Fore.WHITE + ' CTRL+C to go BACK')
file.write(str(j) + ":0x" + f + "\n")
time.sleep(0.15)
j += 1
file.close()
elif x >= 97:
print(console + colorama.Fore.GREEN + "[SUCCESS] " + "0x" + f + ' ETH Wallet' + colorama.Fore.WHITE + ' CTRL+C to go BACK')
file2.write(str(h) + ":0x" + f + "\n")
time.sleep(0.15)
h += 1
file2.close()```
[1]: https://i.stack.imgur.com/Qa7Bw.png

So to print CTRL+C to go BACK only on the last loop iteration, you have too check if h + 1 is going to break the loop condition. To do so, you could check the condition h + 1 >= math.inf.
if h + 1 >= math.inf:
print("CTRL+C to go BACK")
This way you'll have to remove the CTRL+C to go BACK in your infos print functions

Related

Why is the condition performed when it shouldn't be?

I have a sequence of characters that I want to insert into a 2D table. But, despite the if condition : when I reach a character '*', it goes into the if condition, whereas it should go directly into the else.
if str(tab_2D[q + 5][1]) != '*':
print("IN")
if table_transition[int(etat1) + 1][ord(symbole) - ord('a') + 2] == " ":
table_transition[int(etat1) + 1][ord(symbole) - ord('a') + 2] = etat2
else:
table_transition[int(etat1) + 1][ord(symbole) - ord('a') + 2] += ',' + etat2
else: # si le symbole EST epsilon
if table_transition[int(etat1) + 1][int(symbol) + 2] == " ":
table_transition[int(etat1) + 1][int(symbol) + 2] = etat2
else:
table_transition[int(etat1) + 1][int(symbol) + 2] += ',' + etat2
q += 1
Here's the proof that ' * ' does go into the if condition because "IN" is displayed once when it's really a ' * ' character that is being compared. It should not be displayed.
Here's proof:
Do you know why?
I solved the problem, I had forgotten to add q += 1 before the main else

how to feed strings in an empty list?

I am trying to store the values obtained from excel sheet cells to a list. The code provided basically collects data from different continuous rows and columns and creates a string of those values. I could work upt o storing the string value but I don't really know how to store the strings in a list, Can anyone help me with this?
for i in range(NR):
print("This TC checks the output for")
for j in range(NC):
inputVariable = str(ws[get_column_letter(ColumnStart+j) + str(rowStart-1)].value)
c = str((ws.cell(row = (rowStart + i),column = (ColumnStart +j)).value))
if (ws.cell(row = (rowStart + i),column = (ColumnStart+j)).value) == (ws.cell(row = (MaxValRow),column = (ColumnStart+j)).value):
b = '(maximum)'
elif (ws.cell(row = (rowStart + i),column = (ColumnStart+j)).value) == (ws.cell(row = (MinValRow),column = (ColumnStart+j)).value):
b = '(minimum)'
else:
b ='(intermediate)'
Commentstr = str(j+1) + '. The value of input ' + inputVariable + ' =' + " " + c + b
# need to create a list here to store the commentstr for each iteration
NR = no. of rows, NC = no. of columns
my_list=[]
for i in range(NR):
x=0
print("This TC checks the output for")
for j in range(NC):
inputVariable = str(ws[get_column_letter(ColumnStart+j) + str(rowStart-1)].value)
c = str((ws.cell(row = (rowStart + i),column = (ColumnStart +j)).value))
if (ws.cell(row = (rowStart + i),column = (ColumnStart+j)).value) == (ws.cell(row = (MaxValRow),column = (ColumnStart+j)).value):
b = '(maximum)'
elif (ws.cell(row = (rowStart + i),column = (ColumnStart+j)).value) == (ws.cell(row = (MinValRow),column = (ColumnStart+j)).value):
b = '(minimum)'
else:
b ='(intermediate)'
Commentstr = str(j+1) + '. The value of input ' + inputVariable + ' =' + " " + c + b
my_list[x]=Commentstr
x+=1

How to avoid used many if statements in definition?

I have a function which create list of url. Function works fine. But I do not like what this function looks like:
def prepareadres(city,y,m,d):
month = [31,28,31,30,31,30,31,31,30,31,30,31]
d = d + 1
d0 = 0
m0 = 0
if (d > month[0]):
d = d - month[0]
m = m + 1
if (d > month[1]):
d = d - month[1]
m = m + 1
if (d > month[2]):
d = d - month[2]
m = m + 1
if (d > month[3]):
d = d - month[3]
m = m + 1
if (d > month[4]):
d = d - month[4]
m = m + 1
if (d > month[5]):
d = d - month[5]
m = m + 1
if (d > month[6]):
d = d - month[6]
m = m + 1
if (d > month[7]):
d = d - month[7]
m = m + 1
if (d > month[8]):
d = d - month[8]
m = m + 1
if (d > month[9]):
d = d - month[9]
m = m + 1
if (d > month[10]):
d = d - month[10]
m = m + 1
if (d > month[11]):
d = d - month[11]
m = m + 1
if d == sum(month):
print("year complete")
#print(df.iloc[0, :-2])
if (m < 10) and (d < 10):
adres = "https://sinoptik.ua/погода-" + city + "/" + str(y) + "-" + str(m0) + str(m) + "-" + str(d0) + str(d)
elif (m < 10) and (d >= 10):
adres = "https://sinoptik.ua/погода-" + city + "/" + str(y) + "-"+ str(m0) + str(m) + "-" + str(d)
elif (m >= 10) and (d < 10):
adres = "https://sinoptik.ua/погода-" + city + "/" + str(y) + "-" + str(m) + "-" + str(d0) + str(d)
else:
adres = "https://sinoptik.ua/погода-" + city + "/" + str(y) + "-" + str(m) + "-" + str(d)
#print(adres)
return adres
Code looks awful as I mention.I used many if statements and want to simplify this. Can you suggest more elegant ways of writing this definition, avoiding the use of if statements?
Use a for loop to iterate over the months:
def prepareadres(city,y,m,d):
months = [31,28,31,30,31,30,31,31,30,31,30,31]
d = d + 1
d0 = 0
m0 = 0
for month in months:
if d > month:
d = d - month
m += 1
if d == sum(months):
print("year complete")
#print(df.iloc[0, :-2])
if (m < 10) and (d < 10):
adres = "https://sinoptik.ua/погода-" + city + "/" + str(y) + "-" + str(m0) + str(m) + "-" + str(d0) + str(d)
elif (m < 10) and (d >= 10):
adres = "https://sinoptik.ua/погода-" + city + "/" + str(y) + "-"+ str(m0) + str(m) + "-" + str(d)
elif (m >= 10) and (d < 10):
adres = "https://sinoptik.ua/погода-" + city + "/" + str(y) + "-" + str(m) + "-" + str(d0) + str(d)
else:
adres = "https://sinoptik.ua/погода-" + city + "/" + str(y) + "-" + str(m) + "-" + str(d)
#print(adres)
return adres

Python 3 ASCII code 219 issue

I compared code to other training example code out there and did a compare and everything matches. Here is my issue: I have a health bar and when I take damage the bar decreases.
The problem is when in my code I use █ the pipe line after the bar never stays put, it always move dynamically. When I use / the pipe bar always stays put and no issue. I'm guessing there is something in my terminal tool (using Pycharm) that it doesn't like ascii code 219. If it matters from reading forums the tool is set to UTF-8. example below so might not format correctly but top part you can see the | being shifted when using █ and the bottom part is fine when using /.
______________________________ __________
CARLOS: 2210/3260 |■■■■■■■■■■■■■■■■■ | 132/132 |//////////|
__________________ __________
CARLOS: 2219/3260 |///////////////// | 132/132 |//////////|
Code:
def get_stats(self):
hp_bar = ""
bar_ticks = (self.hp / self.maxhp) * 100 / 4
mp_bar = ""
mp_ticks = (self.mp / self.maxmp) * 100 / 10
while bar_ticks > 0:
hp_bar += '█'
bar_ticks -= 1
#num_spaces_needed = (100/4) - len(hp_bar)
#str_spaces_needed = ""
#while num_spaces_needed > 0:
#str_spaces_needed += " "
#num_spaces_needed -= 1
while len(hp_bar) < 25:
hp_bar += " "
while mp_ticks > 0:
mp_bar += "/"
mp_ticks -= 1
while len(mp_bar) < 10:
mp_bar += " "
hp_string = str(self.hp) + "/" + str(self.maxhp)
current_hp = ""
if len(hp_string) < 9:
decreased = 9 - len(hp_string)
while decreased > 0:
current_hp += " "
decreased -= 1
current_hp += hp_string
else:
current_hp = hp_string
mp_string = str(self.mp) + "/" + str(self.maxmp)
current_mp = ""
if len(mp_string) < 7:
decreased = 7 - len(mp_string)
while decreased > 0:
current_mp += " "
decreased -= 1
current_mp += mp_string
else:
current_mp = mp_string
print(" _______________________________ __________ ")
print(bcolors.BOLD + self.name + " " +
current_hp + " |" + bcolors.BAR + hp_bar + bcolors.ENDC + "| " +
current_mp + " |" + bcolors.OKBLUE + mp_bar + bcolors.ENDC + "| ")
You have to change your font type
Navigate to File -> Settings -> Editor -> Font
Font: Source Code Pro
Apply -> Ok
Rerun the program
Try the following at the top of your script:
import sys
reload(sys)
sys.setdefaultencoding('utf8')

how to run a python file 20 times with 1000 iterations

i want to run a python file file.py 20 times with 1000 iterations with single run click so that i dont need to click run 20 times manually.
Init()
globalBest=pop[0].chromosome
# Saving Result
fp=open(resultFileName,"w");
fp.write("Iteration,Fitness,Chromosomes\n")
for i in range(0,iterations):
Crossover()
Mutation()
MemoriseGlobalBest()
if funEval >=maxFunEval:
break
if i%20==0:
print "I:",i,"\t Fitness:", bestFitness
fp.write(str(i) + "," + str(bestFitness) + "," + str(bestChromosome) + "\n")
print "I:",i+1,"\t Fitness:", bestFitness
fp.write(str(i+1) + "," + str(bestFitness) + "," + str(bestChromosome))
fp.close()
You can write another script which call your script 20 times. Make a loop and call the file.py 20 times in it.
try:
for iteration in range(20):
Init()
globalBest = pop[0].chromosome
# Saving Result
fp = open(resultFileName, "a")
fp.write("Iteration,Fitness,Chromosomes\n")
for i in range(0, iterations):
Crossover()
Mutation()
MemoriseGlobalBest()
if funEval >= maxFunEval:
break
if i % 20 == 0:
print
"I:", i, "\t Fitness:", bestFitness
fp.write(str(i) + "," + str(bestFitness) + "," + str(bestChromosome) + "\n")
print
"I:", i + 1, "\t Fitness:", bestFitness
fp.write(str(i + 1) + "," + str(bestFitness) + "," + str(bestChromosome))
fp.close()

Categories

Resources