Whenever I have two conflicting greater than how do I set a limit there - python-3.x

Hi I'm kind of new to programming. I used Game Maker before this so I need help.
Whenever I have two conflicting greater than how do I set a limit there. Example:
if (weight>=20): (print(example))
if (weight>=30): (print(example)) So if I were to type in 35 it would print both things. So how do I make it print only one thing. Thank you for your time.
P.S I'm using python 3.6.

The answer is in the elif statement
if x > a:
# x is greater than a
#do something
pass
elif x > b:
# x is not greater than a, but greater than b
# do something
pass
else:
# do something else
pass

Related

I have tried solving this problem, encountered a maximum recursion depth exceeded error

I am a bit of a noob, please go easy on me.
I'm trying to solve this.
The input I'm using.
So I have written code using mammoth amount of input and it keeps on piling up every time function is called.
I want to understand how to approach this and not get the Maximum Recursion Depth Exceeded Error
count = 0
#a #I assign the whole input here
def count_bag(x):
#a #I assign the whole input here again
for x in a:
if x == "other bags":
continue
elif "shiny gold" in a[x]:
global count
count += 1
break
else:
for y in a[x]:
count_bag(y)
count_bag(a)
print(count)
In python the recursion depth is limited for most compilers, so if you want to do some deep recursion you can use sys.setrecursionlimit(some value) to get over this issue
import sys
sys.setrecursionlimit(10**6)
Why not use a queue and skip the recursion? That way, you don't even need to use a global count variable. I haven't compiled/linted this or anything, but something like this:
def count_bag(x, a):
count = 0
to_process = list()
to_process.extend(x)
while len(to_process) > 0:
item = to_process.pop(0) # pop item off front of list
if item == 'other bags':
continue
elif 'shiny gold' in a[item]:
count += 1
else:
for y in a[item]:
to_process.extend(y)
return count

How to replace a variable with nothing if certain conditions are met using a if statement (Python 3)

I'm trying to make a completing the square calculator. I replicated some of the lengthy code to show where im getting my issue:
a=1
if (a == 1):
print ()
print ("bcdf" + str(a))
Output: bcdf1
In this case, I want it to output bcdf
I genuinely looked all over the place trying to find an answer to this.
I'm not sure if I understand correctly the problem, but probably this is the solution you're searching for:
a = 1
if (a == 1):
print("bcdf")
else:
print("bcdf" + str(a))
You can find more information regarding if..else statements in the documentation.
Since a is a number you could set it to None to reset its value. Also, if you are using Python>3.6 you have f-strings to print what you want nicely in one line:
a = 1
if a == 1:
a = None
print(f"bcdf{a if a is not None else ''}")
This says to evaluate to an empty string if a is None.
If you do not have f-strings you can do it the old way with format, it does exactly the same thing:
a = 1
if a == 1:
a = None
print("bcdf{}".format(a if a is not None else ""))

How to give a Subclip a new name in Python using PyAutoGUI?

Complete beginner here making my first program with PyAutoGui as I cannot get access to the software's API. My issues are currently being that I am unable to come up with a solution to name each subclip with a different appended letter on the end. The naming convention should go like so, MI899001~AA, MI899001~AB, MI899001~AC, MI899001~AD. The only thing that changes is the last letter.
Below is the relevant code I'm currently using for the program I am writing;
def naming_promo():
x = string.ascii_uppercase
pyautogui.typewrite('DNH~P336007A' + x[0][0])
for i in range(7):
if i == 0:
sub_clip_clean()
else:
if i >= 1:
pyautogui.typewrite('567890qwe', 0.2)
sub_clip_package()
naming_promo() # See above Fn for method
pyautogui.moveTo(646, 404, duration=0.50)
pyautogui.click()
move_clips()
The naming_promo() takes the ascii_uppercase and types the first letter. I however can't figure out how to iterate through each character in the string as the For Loop repeats. I've googled many solutions, but I guess I can't get my head around how to do a loop in a loop and increment the x value used each time.
This is my first post so apologies for any etiquette I break. Any help and explanation would be greatly appreciated.
This is my first answer so apologies for any etiquette I break.
I'm not sure I understand everything here, since there's a few functions in the code that I don't know about. However, are you just looking for something like:
def naming_promo(n):
x = string.ascii_uppercase
pyautogui.typewrite('DNH~P336007A' + x[0][n])
and further down in your code, simply create a variable and increment it up one after use:
m = 0
for i in range(7):
if i == 0:
sub_clip_clean()
else:
if i >= 1:
pyautogui.typewrite('567890qwe', 0.2)
sub_clip_package()
naming_promo(m) # See above Fn for method
m += 1
pyautogui.moveTo(646, 404, duration=0.50)
pyautogui.click()
move_clips()

A simpler way to do something for the 1st item of A in C that is also in B without iterating twice

I have constructed the following script in Python 3 that does what it needs to, but, it iterates through my items twice. Is there are way to the same outcome with a single iteration?
if any(A in B for A in C):
for A in C:
if A in B:
# Do something with A.
# Order of iteration is important.
break
else:
# Do something else
for loops can also have else clauses, which they enter if you don't break out of them. So your loop can be written
for A in C:
if A in B:
# Do something
break
else:
# Do something else
The most efficient way would probably to get A in a single iteration at the C-level (using filter and next) and then use it straight away.
A = next(filter(B.__contains__, C), None)
if A is not None:
# Do something with A
else:
# Do something else

Euler 4, Modulo issue

Newbie programmer fresh off of code academy. I wanted to keep going so I started the Euler examples. I've got to no.4, https://projecteuler.net/problem=4:
My program is getting to the number 980089 happily enough but decides that this number is divisible by 994. But the other value is actually 986.0050302. That isn't the answer that I'm looking for. So modulo isn't doing its job because there is a remainder of 0.0050302. Why is this happening?
Full code:
x = 999998
g = 2
while g < 99:
e = str(x)
if e[::-1] == str(x):
print(e)
for f in reversed(range(100, 1000)):
f = float(f)
h = x/f
if int(x) % f == 0.000000 and h < 1000:
print("%s and %s" % (f, h))
break
else:
x = x - 1
else:
x = x - 1
Tips on how I could improve my code would be great too, I don't think my while command was used how I originally imagined.
It looks like the general idea of what you're trying to do is to start at a number larger than anything two 3-digit numbers could multiply to, and then just start going down by one seeing if there are any possible 3-digit numbers that will multiply to your current number.
First thing I notice is that your while loop uses g, but you never modify it inside your loop, so it's rather useless. I also notice you're casting x to an int using int(x) at one point, which is not needed because x will always already be an int.
I'm going to try and rewrite your code to do the right thing - let me know if you have any questions about it!
x = 999998
found_an_answer = False
# We'll certainly find an answer before we get here
while x > 10000:
# get the string version of the number
x_string = str(x)
# See if its the same as its reverse
if x_string == x_string[::-1]:
# Print out the palindrome we're currently looking at
print ("Looking for 3-digit factors of " + x_string)
# Let's try all the factors from 999 to 100
for f in reversed(range(100, 1000)):
# First, check if f is a factor of x by checking if the
# remainder is 0 when we divide x by f.
# Then, check if the other factor, x/f, is less than 1000
# to make sure it is three digits
if x%f == 0 and x/f < 1000:
# If we made it here, we found the number!!
print("Found the number!")
print("Number: {}".format(x))
print("Factors: {}, {}".format(f, x/f))
# We set this to be true because using a "break"
# statement will only break out of the inner most
# loop (the one where we're modifying f
found_an_answer = True
break
# Now that we left the inner loop, we can check the
# found_an_answer variable to see if we should
# break out of the outer loop to.
if found_an_answer:
break
# Guess we didn't find an answer yet. LEt's look at the next
# smallest number.
x = x-1
One more little thing - instead of having using a while loop and manually decreasing x inside it, you could just do a for loop counting down, by changing the loop to be
for x in range(999998, 10000, -1):
Then you don't need the x=999998 line or the x = x-1 line.

Resources