Why is there coming up the SyntaxError with if and elif[ - python

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/')

Related

Multiple If Statement in function not applying properly to Pandas dataframe

I have a table where I am trying to calculate if on a given day a carding attack occurred by looking at the current day's network decline count, network decline rate, and rolling seven day average of network approvals. Below is how I was calculating if it was a carding attack or not. However the results don't match the data.
def carding_attack(row):
if (row['network_decline_rate']>=30 & row['seven_day_average'] >= 681 & row['count_Network Decline']>=(row['seven_day_average']*4)):
return "Carding Attack 1"
elif (row['network_decline_rate']>=30 & row['seven_day_average'] >= 251 & row['seven_day_average'] <= 680 & row['count_Network Decline'] >= 2600):
return "Carding Attack 2"
elif (row['network_decline_rate']>=30 & row['seven_day_average'] >= 151 & row['seven_day_average'] <= 250 & row['count_Network Decline'] >= 2000):
return "Carding Attack 3"
elif (row['network_decline_rate']>=30 & row['seven_day_average'] >= 101 & row['seven_day_average'] <= 150 & row['count_Network Decline'] >= 1500):
return "Carding Attack 4"
elif (row['network_decline_rate']>=30 & row['seven_day_average'] >= 51 & row['seven_day_average'] <= 100 & row['count_Network Decline'] >= 750):
return "Carding Attack 5"
elif (row['network_decline_rate']>=30 & row['seven_day_average'] >= 11 & row['seven_day_average'] <= 50 & row['count_Network Decline'] >= 580):
return "Carding Attack 6"
elif (row['network_decline_rate']>=30 & row['seven_day_average'] >= 1 & row['seven_day_average'] <= 10 & row['count_Network Decline'] >= 250):
return "Carding Attack 7"
else:
return "Not a Carding Attack"
daily_network_counts['carding_attack'] = daily_network_counts.apply(carding_attack,axis=1)
Actual results which do not match what I want
This is what I was expecting it to look like.enter image description here

Can this code be shortened? Custom time status with emojis

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)

discordpy, bot crashs after simulate a lottery

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.

Returns None instead of False

So I'm working on a question on CodingBat, a website that provides JS and Python practice problems. I've encountered a unexpected output. Btw here's the link to the question: https://codingbat.com/prob/p135815 . In theory my code should return False but it returns none when I put print(squirrel_play(50, False))
Code:
def squirrel_play(temp, is_summer):
if is_summer:
if temp <= 100:
if temp >= 60:
return True
elif temp <= 60:
return False
elif temp >= 100:
return False
if not is_summer:
if temp <= 90:
if temp >= 60:
return True
elif temp >= 90:
return False
elif temp <= 60:
return False
when I run that with print(squirrel_play(50, False)), I get None (I should get False)
Why???
With your parameter of is_summer of False, you're in the 2nd conditional block:
if not is_summer:
if temp <= 90:
if temp >= 60:
return True
elif temp >= 90:
return False
elif temp <= 60:
return False
Then follow this block:
is the temp less than 90? yes. so now we're in this block:
if temp <= 90:
if temp >= 60:
return True
What is happening here is that you never get to the elif temp <= 60 because you are in the first conditional instead. You could only ever get to the elif below if you didn't satisfy the first condition.
At the end of this if temp <= 90 block the entire conditional chain ends and your function returns the default value of None because you didn't provide another return value.
You can maybe more clearly see this by making the entire code read:
def squirrel_play(temp, is_summer):
if is_summer:
if temp <= 100:
if temp >= 60:
return True
elif temp <= 60:
return False
elif temp >= 100:
return False
if not is_summer:
if temp <= 90:
if temp >= 60:
return True
else:
return "This is where I'm returning with 50, and True as my parameters"
elif temp >= 90:
return False
elif temp <= 60:
return False
Did you try to debug it?
With squirrel_play(50, False) it will fall into:
def squirrel_play(temp, is_summer):
if is_summer:
if temp <= 100:
if temp >= 60:
return True
elif temp <= 60:
return False
elif temp >= 100:
return False
if not is_summer:
if temp <= 90:
if temp >= 60:
return True
# HERE ( 50 is less than 90 but not greater than 60 )
# and you have no return statement for this case
elif temp >= 90:
return False
elif temp <= 60:
return False
If you don't return a value from a Python function, None is returned by default. I believe that what is happening here is that because you are using elif statements, since the clause if not is_summer: if temp <= 90: is being entered, the final clause elif temp <= 60 is not being reached. Therefore the function gets passed all of the if/elif statements without returning a value, and returns None.
A simple solution is to replace all of the elifs with ifs. Then print(squirrel_play(50, False)) returns False (for me at least).
The way that you have currently coded it, in your
if temp <= 90:
if temp >= 60:
return True
elif ....
if the first if test evaluates True but the second one evaluates False, then no return statement is reached (bear in mind that the subsequent elif tests are not performed because the first if evaluated true), and the function therefore returns None.
In fact you can simplify the function making use of chained comparison operators:
def squirrel_play(temp, is_summer):
if is_summer:
return 60 <= temp <= 100
else:
return 60 <= temp <= 90

I keep getting None

My code:
def get_feedback(mark, out_of):
percentage = int((mark / out_of) * 100)
if percentage >= 80:
print("Excellent")
if 60 < percentage < 70:
print("Good")
if 50 < percentage < 59:
print("Pass")
if percentage < 50:
print("Not a pass")
I know I have to use a return statement somewhere but I'm not really sure how it works or when to use it. If anyone could help, that would be great thank you!
def get_feedback(mark, out_of):
percentage = int((mark / out_of) * 100)
remark = ''
if percentage >= 80:
remark = "Excellent"
elif 60 <= percentage <= 79:
remark = "Good"
elif 50 <= percentage <= 59:
remark = "Pass"
else percentage < 50:
remark = "Not a pass"
return remark
Some suggestions:
I believe you need inclusive range, so include <= instead of <
If one condition satisfies, no need to check the rest of the conditions. So instead of using if for every check, use if - elif- else checks.
Also your question says the range between 60 and 79 for grade 'Good'. You haven't checked it.
Use return in place of print. Example :- return "Excellent".
Another way to do it :
def get_feedback(mark, out_of):
percentage = int((mark / out_of) * 100)
if percentage >= 80:
return "Excellent"
elif 60 <= percentage <= 79:
return "Good"
elif 50 <= percentage <= 59:
return "Pass"
else:
return "Not a pass"
You can make a variable to represent the value of its condition.
def get_feedback(mark, out_of):
percentage = int((mark / out_of) * 100)
if percentage >= 80:
feedback = "Excellent"
elif 60 < percentage < 70:
feedback = "Good"
elif 50 < percentage < 59:
feedback = "Pass"
elif percentage < 50:
feedback = "Not a pass"
return print(feedback)
At the very end, we use return to give us the result of the function. Also, notice that I used elif statements, which are faster than just using if statements.

Categories

Resources