How to give a Subclip a new name in Python using PyAutoGUI? - python-3.x

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()

Related

Function keeps looping same two numbers

So i'm trying to follow along in the Automate The Boring Stuff with Python book. There's a question that asks me to make the collatz sequence. One function collatz() will be processing the actual math, while the second function get_collatz() will be the one taking user input and running it through the collatz function until the returned number == 1. I apologize if this is a simple mistake, I may have had a little to drink as well as being new to the site and fairly new to coding also. Would appreciate it if someone could help to point me in the right direction. I don't want a straight up answer because I want to learn myself, would just like an idea of what could be going wrong here. Here is my code.
def collatz(number):
if number % 2 == 0:
number //= 2
print(number)
return number
else:
number = 3 * number + 1
print(number)
return number
def get_collatz():
number = int(input('Put in a number please!: '))
while number != 1:
print(number)
collatz(number)
get_collatz()
Asking only for a hint is not really how stackoverflow questions are meant to be. But if a hint you want, a hint you get :)
def get_collatz():
number = int(input('Put in a number please!: '))
while number != 1:
print(number)
collatz(number)
Your variable, number, is a simple integer. It changes its value only when you use an operation like
number = <something>
number += 2
Hint: You don't seem to use any of these operation inside your implementation of get_collatz(). Not other than the initial input, at least :).

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 ""))

Why does the "AddOre" Function doesn't work?

I have been reading the book "Automate the Boring Stuff with Python" and trying to learn Python on my own, I decided to practice what I've learned in chapter one and got a little bit confused by this code and why it doesn't work.
Been sitting and trying to figure it out but still didn't make it.
I've tried to switch the given values of the function and other stuff as well but if I am being honest I don't really know what I am doing I will be very glad if someone explains causes the problem and how to fix it, here's the code :
Mine = {'Bronze': 10,'Iron': 40,'Gold': 2,'Diamonds':2}
def ShowMine(cave):
for k, v in cave.items():
Total_Res = 0
print("There is : " + str(k))
print("And there is : " + str(v) + " of it")
Total_Res += int(v)
print("total ores in the cave : " + str(Total_Res))
def AddOre(cave, ore):
for k,v in cave.items():
cave += ore.values()
Mine1 = {'Bronzze': 10,'Iron': 0,'Gold': 2,'Diamonds':4}
ShowMine(Mine)
AddOre(Mine,Mine1)
ShowMine(Mine)
I expect the code to output the added values from Mine1 to the original Mine created at the start of the program.
The two arguments cave and ore of the function AddOre are both dictionaries. The function then iterates over the items of the first arguments and in each iteration tries to add the return value of ore.values(), which is a list, to the dictionary cave. This doesn't work and I'm not sure what the result of that operation is even supposed to be.
What you want to do, I'm guessing, is to add the values of the second argument to the values of the first argument identifying them by their keys. This can be done by this code:
def AddOre(cave, ore):
for k, _ in cave.items():
if k in ore:
cave[k] += ore[k]
for k, v in ore.items():
if k not in cave:
cave[k] = v
The first loop adds the values of the ores that already exist in the cave and the second loop adds all the new ores. There might be a simpler way to do the same thing with native addition operations of list and dictionary types, but I can't think of any right now.

Equivalent while loop block for a for loop block

I am a novice trying to learn python from Automate The Boring Stuff with Python by Al Sweigart and I came across his block of code to answer a math problem: "What is the sum of all the numbers from 0 to 100?" Apparently, this was a question Gauss got when his teacher wanted to keep him busy.
Sweigart used a for loop and range() function to get the answer:
total = 0
for num in range(101):
total=total+num
print(total)
A page later he states that "you can actually use a while loop to do the same thing as a for loop; for loops are more concise."
How would this statement be rendered in a while loop?
I tried replacing the for with while but got an error: "name 'num' is not defined." I also tried to set up a summation math equation using another block of code from another forum, but completely got lost.
print('Gauss was presented with a math problem: add up all the numbers from 0 to 100. What was the total?')
a=[1,2,3,4,5,...,100]
i=0
while i< len(a)-1:
result=(a[i]+a[i+1])/2
print(result)
i +=1
Then, I tried to setup i in an equation that would loop until each number was added, but got stuck.
print('Gauss was presented with a math problem: add up all the numbers from 0 to 100. What was the total?')
i=0
while i<101:
i=i+1
a=i
Would the while statement be too complex to warrant the effort?
Your last example comes close.
A for loop of this form:
for x in range(N):
# ...
can be replaced by a while loop like this:
x = 0
while x < N:
# ...
x += 1 # equivalent to x = x + 1
Just make sure you leave the rest of the code unchanged!
The for loop is more concise. Notice how we need a "counter" variable , in this case i with the while loop. That's not to say we don't need them in a for loop, however they're integrated nicely into the syntax to make for cleaner code.
i = 0
total = 0
while i < 101:
total += i
i += 1
print(total)
Python's for loop syntax is also a foreach equivalent:
for eachItem in list:
# Do something

Python: find the first even number in a list using a while loop

I'm trying to write a function that returns the first even number in a list, using a while loop
def first_even(list):
while i%2 !=0: #I'm not sure if this is a good start for the while loop, but I think I'm supposed to use something%2...
#no idea what to do here
A good and very short coding style will be:
for i in list:
if i%2 == 0:
return i

Resources