I am trying to create a custom status where you can see the time in real time, and the emoji of the clock indicating that same time, I have done it, and it is functional, but I would like to know if there is any way to abbreviate this code, or is it already as efficient as possible? Thanks
import time
emoji = ""
emojis = ["๐", "๐", "๐", "๐", "๐", "๐",
"๐", "๐", "๐", "๐ ", "๐", "๐ก",
"๐", "๐ข", "๐", "๐ฃ", "๐", "๐ค",
"๐", "๐ฅ", "๐", "๐ฆ", "๐", "๐ง"]
while True:
hour= time.strftime('%H:%M:%S')
hm = int(time.strftime('%I')) + int(time.strftime('%M'))/100
if 1 <= hm < 1.30:
emoji = emojis[0]
elif 1.30 <= hm < 2:
emoji = emojis[1]
elif 2 <= hm < 2.30:
emoji = emojis[2]
elif 2.30 <= hm < 3:
emoji = emojis[3]
elif 3 <= hm < 3.30:
emoji = emojis[4]
elif 3.30 <= hm < 4:
emoji = emojis[5]
elif 4 <= hm < 4.30:
emoji = emojis[6]
elif 4.30 <= hm < 5:
emoji = emojis[7]
elif 5 <= hm < 5.30:
emoji = emojis[8]
elif 5.30 <= hm < 6:
emoji = emojis[9]
elif 6 <= hm < 6.30:
emoji = emojis[10]
elif 6.30 <= hm < 7:
emoji = emojis[11]
elif 7 <= hm < 7.30:
emoji = emojis[12]
elif 7.30 <= hm < 8:
emoji = emojis[13]
elif 8 <= hm < 8.30:
emoji = emojis[14]
elif 8.30 <= hm < 9:
emoji = emojis[15]
elif 9 <= hm < 9.30:
emoji = emojis[16]
elif 9.30 <= hm < 10:
emoji = emojis[17]
elif 10 <= hm < 10.30:
emoji = emojis[18]
elif 10.30 <= hm < 11:
emoji = emojis[19]
elif 11 <= hm < 11.30:
emoji = emojis[20]
elif 11.30 <= hm < 12:
emoji = emojis[21]
elif 12 <= hm < 12.30:
emoji = emojis[22]
elif 12.30 <= hm < 13:
emoji = emojis[23]
print(f"{emoji} {hour}")
time.sleep(1)
I'll be a little off on the indices, however the following approach has some improvements.
import time
emojis = "๐๐๐๐๐๐๐๐๐๐ ๐๐ก๐๐ข๐๐ฃ๐๐ค๐๐ฅ๐๐ฆ๐๐ง"
while True:
hour= time.strftime('%H:%M:%S')
hm = int(time.localtime().tm_hour + time.localtime().tm_min / 30 + 0.5)
print(f"{emojis[hm]} {hour}")
time.sleep(1)
Related
The pseudo-hyperwebster should have exactly 17567 resulting strings after running on three characters. However, it only gives me 92% of the results I should be getting at 16300 strings.
This is the code I used:
abcs = ["a", "b", "c", "d", "e", "f", "g", \
"h", "i", "j", "k", "l", "m", "n", "o", "p", \
"q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
entrydict = {}
#Variables
ites = 0
char1 = 0
char2 = 0
char3 = 0
char4 = 0
char5 = 0
char6 = 0
char7 = 0
char8 = 0
char9 = 0
char10 = 0
char11 = 0
char12 = 0
char13 = 0
char14 = 0
char15 = 0
word_guess = ""
################################################################################
#Actual Cracker
word_guess = abcs[char1] + abcs[char2] + abcs[char3] + abcs[char4] \
+ abcs[char5] + abcs[char6] + abcs[char7] + abcs[char8] + abcs[char9] \
+ abcs[char10] + abcs[char11] + abcs[char12] + abcs[char13] + abcs[char14] \
+ abcs[char15]
while True:
if char15 < 25:
char15 += 1
elif char14 < 25:
char14 += 1
char15 -= 25
elif char13 < 25:
char13 += 1
char14 -= 25
elif char12 < 25:
char12 += 1
char13 -= 25
elif char11 < 25:
char11 += 1
char12 -= 25
elif char10 < 25:
char10 += 1
char11 -= 25
elif char9 < 25:
char9 += 1
char10 -= 25
elif char8 < 25:
char8 += 1
char9 -= 25
elif char7 < 25:
char7 += 1
char8 -= 25
elif char6 < 25:
char6 += 1
char7 -= 25
elif char5 < 25:
char5 += 1
char6 -= 25
elif char4 < 25:
char4 += 1
char5 -= 25
elif char3 < 25:
char3 += 1
char4 -= 25
elif char2 < 25:
char2 += 1
char3 -= 25
elif char1 < 25:
char1 += 1
char2 -= 25
word_guess = abcs[char1] + abcs[char2] + abcs[char3] + abcs[char4] \
+ abcs[char5] + abcs[char6] + abcs[char7] + abcs[char8] + abcs[char9] \
+ abcs[char10] + abcs[char11] + abcs[char12] + abcs[char13] + abcs[char14] \
+ abcs[char15]
ites += 1
if ites < 1000000:
entrydict[ites] = word_guess
if char13 == 25:
break
################################################################################
print(f'There were {str(ites)} iterations recognized.')
while True:
try:
index = int(input("Give an index: "))
print(f'Index {index} contains: {entrydict[index]}')
except IndexError:
print("Whoops!, IndexError")
continue
except TypeError:
print("Whoops!, TypeError")
continue
except ValueError:
print("Whoops!, ValueError")
continue
except KeyError:
print("Whoops!, KeyError")
continue
I tried modifying the numbers to make it generate everything, although it may be a limitation of the hardware I am using. I also went through and troubleshot all the variables and equations. I expected it to change, but nothing was different.
If you want a list of all 3-letter combinations of lower-case letters, there's no need to complicate things. (I assume, like Tim said in the comments, that you have an off-by-one error in your original code.)
import string
abcs = string.ascii_lowercase
entries = []
for ch1 in abcs:
for ch2 in abcs:
for ch3 in abcs:
entries.append(ch1 + ch2 + ch3)
print(len(entries))
This happily prints out 17576 (26 ** 3).
This can be further simplified as
import itertools
import string
entries = list(itertools.product(string.ascii_lowercase, repeat=3))
print(len(entries))
if you like, which also allows you to increase the N by just changing that 3 to something else.
I am attempting to create an exam grading program. I successfully wrote each function to do what I need it to, but when I attempt to execute the entire program, I am running into issues with my return variables not being referenced.
Here is my code for context:
def userinput():
allinputs = []
while True:
try:
results = list(map(int, input('Exam points and exercises completed: ').split(' ')))
allinputs.append(results)
except:
ValueError
break
return allinputs
def points(allinputs):
exampoints = []
exercises = []
exercises_converted = []
gradepoints = []
counter = 0
for i in allinputs:
exampoints.append(i[0])
exercises.append(i[1])
for e in exercises:
exercises_converted.append(e//10)
for p in exampoints:
p2 = exercises_converted[counter]
gradepoints.append(p + p2)
counter += 1
return (gradepoints, exampoints, exercises_converted)
def average(gradepoints):
avg_float = sum(gradepoints) / len(gradepoints)
points_avg = round(avg_float, 1)
return points_avg
def pass_percentage(exercises_converted, exampoints):
failexam = []
passexam = []
passorfail = []
i = 0
while i < len(exampoints):
if exampoints[i] < 10 or exercises_converted[i] + exampoints[i] < 15:
failexam.append("fail")
passorfail.append(0)
else:
passexam.append("pass")
passorfail.append(1)
i += 1
percentage_float = len(passexam)/len(failexam)
percentage = round(percentage_float, 1)
return (percentage, passorfail)
def grades_distribution(passorfail, gradepoints):
grades = []
i = 0
l = 5
while i < len(gradepoints):
if passorfail[i] == 0:
grades.append(0)
elif passorfail[i] != 0 and gradepoints[i] >= 15 and gradepoints[i] <= 17:
grades.append(1)
elif passorfail[i] != 0 and gradepoints[i] >= 18 and gradepoints[i] <= 20:
grades.append(2)
elif passorfail[i] != 0 and gradepoints[i] >= 21 and gradepoints[i] <= 23:
grades.append(3)
elif passorfail[i] != 0 and gradepoints[i] >= 24 and gradepoints[i] <= 27:
grades.append(4)
elif passorfail[i] != 0 and gradepoints[i] >= 28 and gradepoints[i] <= 30:
grades.append(5)
i += 1
while l >= 0:
print(l, ": ", "*" * grades.count(l))
return
userinput()
print("Statistics:")
points(allinputs)
print(f"Points average: {average(gradepoints)}")
print(f"Pass percentage: {pass_percentage(exercises_converted, exampoints)}")
print("Grade distribution")
grades_distribution(passorfail, gradepoints)
I have no problems with the mechanics within each function; rather, my error lies where I try calling each of them from the main function.
The user input() function runs fine, and returns allinputs. However, when the second function points(allinputs) is called, the program states that the argument is undefined.
You should store the return value of a function before passing it as argument.
This should solve your problem
allinputs = userinput()
points(allinputs)
I made this simulation as a discordpy cog, but the bot goes offline and the console is open and don't do anything if I write or quit it...
At an amount of 15000 the bot crashes, what can I do, why it crashs?
There are discord emojies, which are selected randomly and there are different chances with the numbers etc. I hope somebody can help me here!
#bot.command()
async def simulate(self, ctx, amount):
wnitro = 0
wkey = 0
wgold = 0
wred = 0
wblue = 0
wgreen = 0
wgrey = 0
for zaehler in range(1, int(amount)):
drehungen = randint(5, 20)
gone = randint(1, 1000)
gtwo = randint(1, 1000)
gthree = randint(1, 1000)
gfour = randint(1, 1000)
gfive = randint(1, 1000)
gsix = randint(1, 1000)
gseven = randint(1, 1000)
geight = randint(1, 1000)
gnine = randint(1, 1000)
randomitem = [gone, gtwo, gthree, gfour, gfive, gsix, gseven, geight, gnine]
slots = []
for item in range(len(randomitem)):
if randomitem[item] >= 950:
slots.append("<a:classic:802844186026049546>")
elif randomitem[item] >= 850:
slots.append("<a:geld:770235576539676682>")
elif randomitem[item] >= 800:
slots.append("<a:goldendia:802976550995755019>")
elif randomitem[item] >= 650:
slots.append("<a:darkbluedia:802976435500875836>")
elif randomitem[item] >= 500:
slots.append("<a:reddia:802873281841463296>")
elif randomitem[item] >= 300:
slots.append("<a:greendia:802875898353156138>")
else:
slots.append("<a:greydia:802977070627553311>")
cdrehung = 1
nitroextra = randint(1, 100)
keyextra = randint(1, 10)
coinsextra = randint(1, 5)
coins2extra = randint(1, 2)
i = 0
while i < drehungen:
if cdrehung == 0:
gewinn = slots[4]
elif cdrehung == 1:
gewinn = slots[5]
elif cdrehung == 2:
gewinn = slots[6]
elif cdrehung == 3:
gewinn = slots[7]
elif cdrehung == 4:
gewinn = slots[8]
elif cdrehung == 5:
gewinn = slots[0]
elif cdrehung == 6:
gewinn = slots[1]
elif cdrehung == 7:
gewinn = slots[2]
elif cdrehung == 8:
gewinn = slots[3]
cdrehung -= 9
cdrehung += 1
i += 1
if i == drehungen:
if gewinn == "<a:classic:802844186026049546>":
if nitroextra == 1:
wnitro += 1
else:
drehungen += 1
elif gewinn == "<a:geld:770235576539676682>":
if keyextra == 1:
wkey += 1
else:
drehungen += 1
elif gewinn == "<a:goldendia:802976550995755019>":
if coinsextra == 1:
wgold += 1
else:
drehungen += 1
elif gewinn == "<a:darkbluedia:802976435500875836>":
if coins2extra == 1:
wblue += 1
else:
drehungen += 1
elif gewinn == "<a:reddia:802873281841463296>":
wred += 1
elif gewinn == "<a:greendia:802875898353156138>":
wgreen += 1
elif gewinn == "<a:greydia:802977070627553311>":
wgrey += 1
await ctx.send(f"There are the results out of `{str(amount)}x` spins: {str(wnitro)}x Nitro, {str(wkey)} Key, {str(wgold)} <a:goldendia:802976550995755019>, {str(wblue)} <a:darkbluedia:802976435500875836>, {str(wred)} <a:reddia:802873281841463296>, {str(wgreen)}<a:greendia:802875898353156138>, {str(wgrey)}<a:greydia:802977070627553311>")
thanks to everyone who helps c:
Update: I take it back, theoretically your slots could all be the same thing, or a combination of things that cause drehungen to always implement. You really need to reconsider the logic on your loop.
Update: actually that is false, you do update cdrehung which updates gewinn... this is pretty convoluted, but it does seems like it should have to end at some point. That being said this loop is a bit complicated, I would consider some debug messages for all these different variables to figure out what is going on.
Just looking at it, inside your while loop the state of gewinn doesn't change, or any of the other items (wnitro wkey wgold wblue)... sooo if that is the case it could mean that drehungen is always being incremented... which means i == drehungen is always true... which means an infinite loop.
I am trying to make a quiz in python. But the prizes cause errors.
I tried deleting the red spot but it changed location.
if (raha <= 1000):
webbrowser.open('http://www.rrrgggbbb.com')
elif (2500 >= raha = > 1000):
webbrowser.open('https://codepen.io/akm2/full/rHIsa')
elif (3000 >= raha = > 2500):
webbrowser.open('https://turbo.fish/')
elif (4000 >= raha = > 4500):
webbrowser.open('https://turbo.fish/')
elif (3000 >= raha = > 4000):
webbrowser.open('https://hooooooooo.com/')
elif (4500 >= raha = > 6000):
webbrowser.open('https://trypap.com/')
elif (6000 >= raha = > 8000):
webbrowser.open('https://chrismckenzie.com/')
elif (8000 <= raha):
print("sa vรตid auhinna ise valida. Keri alla")
time.sleep(2)
webbrowser.open('https://makefrontendshitagain.party/')
it should not say bad file descriptor
The error is in comparison, not in if-elif. It has to be >= or <=. => and =< are invalid. In some places, you have also put a space between = and >. So, >= is valid but > =" is not.
Use this:
if raha <= 1000:
webbrowser.open('http://www.rrrgggbbb.com')
elif 2500 >= raha >= 1000:
webbrowser.open('https://codepen.io/akm2/full/rHIsa')
elif 3000 >= raha >= 2500:
webbrowser.open('https://turbo.fish/')
elif 4000 >= raha >= 4500:
webbrowser.open('https://turbo.fish/')
elif 3000 >= raha >= 4000:
webbrowser.open('https://hooooooooo.com/')
elif 4500 >= raha >= 6000:
webbrowser.open('https://trypap.com/')
elif 6000 >= raha >= 8000:
webbrowser.open('https://chrismckenzie.com/')
elif 8000 <= raha:
print("sa vรตid auhinna ise valida. Keri alla")
time.sleep(2)
webbrowser.open('https://makefrontendshitagain.party/')
At the moment, I managed to code, successfully, a simulation for a work that I need to do. However, I'm fairly new to python. And so, I'm now in the process of making the simulation more efficient.
For instance:
if random.random() < mm:
z = numpy.random.choice(pat)
if random.random() < 0.5:
if random.random() < mut:
if maleadult[z][0] == 0:
malejuv[y][x][0] = 1
elif maleadult[z][0] == 1:
malejuv[y][x][0] = 0
else:
malejuv[y][x][0] = maleadult[z][0]
else:
if random.random() < mut:
if femaleadult[z][0] == 0:
malejuv[y][x][0] = 1
elif femaleadult[z][0] == 1:
malejuv[y][x][0] = 0
else:
malejuv[y][x][0] = femaleadult[z][0]
if random.random() < 0.5:
if random.random() < mut:
if maleadult[z][1] == 0:
malejuv[y][x][1] = 1
elif maleadult[z][1] == 1:
malejuv[y][x][1] = 0
else:
malejuv[y][x][1] = maleadult[z][1]
else:
if random.random() < mut:
if femaleadult[z][1] == 0:
malejuv[y][x][1] = 1
elif femaleadult[z][1] == 1:
malejuv[y][x][1] = 0
else:
malejuv[y][x][1] = femaleadult[z][0]
where:
mm - male dispersal,
mf - female dispersal,
mut - mutations,
pat - patch,
maleadult - adult male,
femaleadult - adult female,
malejuv - juvenile male,
femalejuv - juvenile female.
As you can see, the code is big. And this is only for males and when they disperse. The rest of the code is very similar. These are standard genetic and demographic processes - but I feel like this can be improved. I feel like these processes are simple enough, so maybe code as big as this is not necessary.
Does anyone have any ideas to shorten this and, by consequence, making it more efficient?
Your example does not have any loops but it looks like it could be simplified by one:
if random.random() < mm:
z = numpy.random.choice(pat)
for i in range(2):
if random.random() < 0.5:
if random.random() < mut:
if maleadult[z][i] == 0:
malejuv[y][x][i] = 1
elif maleadult[z][i] == 1:
malejuv[y][x][i] = 0
else:
malejuv[y][x][i] = maleadult[z][i]
else:
if random.random() < mut:
if femaleadult[z][i] == 0:
malejuv[y][x][i] = 1
elif femaleadult[z][i] == 1:
malejuv[y][x][i] = 0
else:
malejuv[y][x][i] = femaleadult[z][i]
It is also possible to pass a mutable object as reference to a function which can modify it, which allows further reduction of almost redundant code. I've added some data to test it:
#!python3
#coding=utf-8
import random
maleadult = [[["male adult"], ["another male adult"], ]]
femaleadult = [[["female adult"], ["another female adult"], ]]
malejuv = [[[["male juv"],["another male juv"]]]]
mut = 0.5
mm = 1.0
x = 0
y = 0
z = 0
def some_logic(a, j):
""" does something """
if random.random() < mut:
if a[z][i] == 0:
j[y][x][i] = 1
elif a[z][i] == 1:
j[y][x][i] = 0
# added!
else:
j[y][x][i] = 0
else:
j[y][x][i] = a[z][i]
if random.random() < mm:
z = 0 #numpy.random.choice(pat)
for i in range(2):
print(i)
if random.random() < 0.5:
some_logic(maleadult, malejuv)
else:
some_logic(femaleadult, malejuv)
print(maleadult)
print(malejuv)
print(femaleadult)